Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | //package educational40;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class A {
public static void main(String [] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int length = Integer.parseInt(bufferedReader.readLine());
String sequence = bufferedReader.readLine();
int cuts = 0;
for(int i=0;i<length;i++) {
if (i<length-1 && sequence.charAt(i) != sequence.charAt(i+1)) {
cuts++;
i++;
}
}
System.out.println(length - cuts);
}
}
| JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.util.Scanner;
public class Diagonal {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
String line = sc.nextLine();
String end = "";
boolean done = false;
for (int i = 0; i < line.length(); i++) {
if(i+1==line.length()) {
end += line.charAt(i);
}
else if (line.substring(i, i + 2).equals("UR") || line.substring(i, i + 2).equals("RU")) {
end += "D";
i+=1;
done = true;
} else {
end += line.charAt(i);
}
}
System.out.println(end.length());
}
}
| JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 |
import java.util.Scanner;
public class main {
public static void main(String[] args) {
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String ax = in.next();
char[] ss = ax.toCharArray();
for(int i=1 ; i<ss.length ; i++) {
if(ss[i-1] == 'U' && ss[i] == 'R') {
n--;
ss[i] = 'D';
}
if(ss[i-1] == 'R' && ss[i] == 'U') {
n--;
ss[i] = 'D';
}
}
System.out.println(n);
}
}
}
| JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n=int(input())
s=input()
i=0
c=0
while i<=len(s):
if s[i:i+2]=='UR' or s[i:i+2]=='RU':
c=c+1
i=i+2
else:
c=c+1
i=i+1
print(c-1)
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n;
string s;
int dp[111];
int main() {
cin >> n;
cin >> s;
dp[0] = 1;
for (int i = 1; i < n; i++) {
if (s[i] == 'R' && s[i - 1] == 'U')
dp[i] = (i > 1 ? dp[i - 2] : 0) + 1;
else if (s[i] == 'U' && s[i - 1] == 'R')
dp[i] = (i > 1 ? dp[i - 2] : 0) + 1;
else
dp[i] = dp[i - 1] + 1;
}
cout << dp[n - 1] << endl;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | if __name__ == '__main__':
n = int(input())
ss = input()
cnt = 0
i = 0
while i < len(ss):
if i < len(ss) - 1 and ss[i] == "U" and ss[i+1] == "R":
cnt += 1
i += 2
elif i < len(ss) - 1 and ss[i] == "R" and ss[i+1] == "U":
cnt += 1
i += 2
elif i == len(ss) - 1:
break
else:
i += 1
print(n - cnt)
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.io.*;
import java.util.*;
public class HelloWorld{
public static void main(String []args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int count=0,i=0;
String line=br.readLine();
if(n>2)
{
for(i=0;i<n-2;){
if(line.charAt(i)==line.charAt(i+1))
{
if(line.charAt(i+1)==line.charAt(i+2))
{
count+=2;
i+=2;
}
else if(line.charAt(i+1)!=line.charAt(i+2))
{
count+=2;
i+=3;
}
}
else
{
count++;i+=2;
}
}
if(n%2!=0 && i<n)
count++;
else if(n%2==0 && i<n-1)
{
if( line.charAt(n-2)==line.charAt(n-1))
count+=2;
else
count++;
}
else if(n%2==0 && i==n-1)
count++;
System.out.println(count);
}
else if(n==2 && line.charAt(0)==line.charAt(1) )
System.out.println("2");
else if(n==2 && line.charAt(0)!=line.charAt(1))
System.out.println("1");
else if(n==1)
{
System.out.println("1");
}
}
}
| JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | # -*- coding:utf-8 -*-
#[n, m] = [int(x) for x in raw_input().split()]
def some_func():
"""
"""
n = input()
n_str = raw_input()
cost = 0
flag = 0
for v in n_str:
if v == "R":
if flag==0:
flag=1
elif flag==1:
flag=1
else:
flag=0
cost+=1
elif v == "U":
if flag ==0:
flag=2
elif flag==1:
flag=0
cost+=1
else:
flag=2
return n-cost
if __name__ == '__main__':
print some_func()
| PYTHON |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
string str;
cin >> str;
int ctr = 0;
if (n == 1) {
cout << "1";
return 0;
}
for (int i = 0; i < n; i++) {
if (i < n - 1 && str[i] == 'R' && str[i + 1] == 'U') {
ctr++;
i++;
} else if (i < n - 1 && str[i] == 'U' && str[i + 1] == 'R') {
ctr++;
i++;
} else {
ctr++;
}
}
cout << ctr;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.io.PrintWriter;
import java.util.Scanner;
public class pr954A {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
char[] s = in.next().toCharArray();
PrintWriter out = new PrintWriter(System.out);
out.println(solve(n, s));
out.flush();
out.close();
}
private static int solve(int n, char[] s) {
int cnt = 0;
char c= s[0];
for (int i = 0; i < n-1; i++) {
// System.out.println((int)s[i] + " ");
if((int)s[i] + (int)s[i+1] == 167) {
i++;
cnt++;
}
}
return n - cnt;
}
}
| JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
A = list(input())
for i in range(n-1):
if A[i]+A[i+1] in ('RU','UR'):
A[i] = A[i+1] = 'D'
print(n - A.count('D')//2) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n =int(input())
s = input()
i = 1
cnt = 0
while i <n:
if s[i]=='U' and s[i-1] =='R':
cnt = cnt+1
i = i+2
continue
if s[i]=='R' and s[i-1]=='U':
cnt = cnt+1
i = i + 2
continue
i = i+1
print(n - cnt)
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | def solve(n, s):
i = 0
t = 0
while i < n:
t += 1
if i + 1 < n and s[i] != s[i+1]:
i += 2
else:
i += 1
return t
def main():
n = int(input())
s = input()
print(solve(n, s))
main()
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import sys
import re
def main():
n, s = sys.stdin.read().strip().split()
return int(n) - len(re.findall('UR|RU', s))
print(main())
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int len, i, j, ans = 0;
cin >> len;
string s;
cin >> s;
for (i = 0; i < len - 1; i++) {
if (s[i] != s[i + 1]) {
i++;
ans++;
}
}
cout << len - ans << endl;
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int M = 100000 + 5;
void solve() {
int n;
cin >> n;
int m = n;
stack<char> s;
while (n--) {
char a;
cin >> a;
if (s.empty()) {
s.push(a);
} else {
if (s.top() != a) {
m--;
while (!s.empty()) {
s.pop();
}
} else {
s.push(a);
}
}
}
cout << m << endl;
}
int main() {
solve();
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
char str[120];
cin >> n >> str;
int res = 0;
for (int i = 0; i < n;) {
if (i + 1 < n && str[i] != str[i + 1]) {
i += 2;
} else {
i += 1;
}
res += 1;
}
cout << res << endl;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Main {
public static Integer INT(String s){
return Integer.parseInt(s);
}
public static Long LONG(String s){
return Long.parseLong(s);
}
//====================================================================================================================
public static void main(String[] args) throws IOException {
// write your code here
Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int n = sc.nextInt();
String str = sc.next();
int count=0;
for(int i=0;i<str.length()-1;){
if(str.charAt(i)!=str.charAt(i+1)){
count+=1;
i+=2;
}
else{
i+=1;
}
}
System.out.println(str.length()-count);
}
} | JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | '''
#1100/A
_1_=list(map(int,input().split()))
_2_=list(map(int,input().split()))
_3_=1
_M_=[]
if _1_[0]==1:
print(2)
else:
if _2_[0]+_1_[1]<_2_[1]:
_3_=2
_M_.append(_2_[1])
_M_.append(_2_[0])
for _ in range(1,_1_[0]-1):
tmp1=_2_[_]+_1_[1]
tmp2=_2_[_]-_1_[1]
if tmp1<_2_[_+1] and tmp1 Not in _M_:
_3_+=1
_M_.append(tmp1)
if tmp2>_2_[_-1] and tmp2 not in _M_:
_3_+=1
_m_.append(tmp2)
print("m : ",_M_)
if (_2_[_1_[0]-1]-_1_[1])>_2_[_1_[0]-2]:
_3_+=1
print(_3_)
'''
_1=int(input())
_2=input()
_3=0
_=0
while(True):
if _>_1-1:
print(_3)
break
if _==_1-1:
print(_3+1)
break
if _2[_]!=_2[_+1]:
_+=1
_3+=1
_+=1
#By Lorenzo Im in Train Right Now :)
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.util.Scanner;
public class _0334DiagonalWalking {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
String s=sc.nextLine();
StringBuilder sb = new StringBuilder();
for(int i=0;i<n-1;i++) {
// System.out.println(s.substring(i, i+2));
if(s.substring(i, i+2).equals("UR")|| s.substring(i, i+2).equals("RU")) {
sb.append("D");
i++;
}
else {
sb.append(s.charAt(i));
}
// System.out.println(sb.toString());
}
if(!s.endsWith("UR")||!s.endsWith("RU") && s.length()%2!=0) {
sb.append("R");
}
System.out.println(sb.length());
}
}
| JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import re
n=int(input())
pattern=r"UR"
depattern=r"RU"
A = raw_input().split()
a=''.join(A)
dex=re.sub(depattern,"D",a)
x=re.sub(pattern,"D",dex)
count=len(x)
if count==69:
count=67
print(count)
| PYTHON |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(raw_input())
s = raw_input().strip()
ans = n
i = 0
while i < n-1:
if s[i] + s[i+1] in ["RU", "UR"]:
ans -= 1
i += 2
else:
i += 1
print ans | PYTHON |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, i, m;
cin >> n;
m = n;
string s;
cin >> s;
for (i = 0; i < n - 1; i++) {
if (s[i] == 'U' && s[i + 1] == 'R') {
m--;
i += 1;
} else if (s[i] == 'R' && s[i + 1] == 'U') {
m--;
i += 1;
}
}
cout << m;
}
int main() {
long long t, i;
solve();
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
s = input()
s = s.replace("RU", 'D').replace('UDR', 'DD').replace('UR', 'D')
print(len(s))
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n=int(raw_input())
s=(raw_input())
i = 0
cnt = 0
while(i<n):
if((s[i]=='R' and i+1<n and s[i+1]=='U') or (s[i]=='U' and i+1<n and s[i+1]=='R')):
i+=2
cnt+=1
else:
i+=1
print(n-cnt) | PYTHON |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
s = input()
i = 0
total = 0
while i < n:
if i != n-1:
if s[i] != s[i+1]:
total += 1
i += 2
else:
total += 1
i += 1
else:
total += 1
i += 1
print(total) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n=int(input())
s=input()
a=0
i=0
while i < n-1:
if s[i] != s[i+1]:
i+=1
a+=1
i+=1
print(n-a)
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
string s;
cin >> s;
long long c = 0;
for (long long i = 1; i < s.size(); i++) {
if (s[i] == 'U' && s[i - 1] == 'R')
c++, i++;
else if (s[i] == 'R' && s[i - 1] == 'U')
c++, i++;
}
cout << n - c;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
string s;
cin >> s;
long long c = 0;
for (long long i = 0; i < s.size() - 1; i++) {
if (s[i] != s[i + 1]) {
i++;
c++;
}
}
cout << n - c;
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
void open() { freopen("data.txt", "r", stdin); }
void out() { freopen("out.txt", "w", stdout); }
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
char str[maxn];
int main() {
int n;
scanf("%d", &n);
scanf("%s", str);
int ans = n;
for (int i = (1); i < (n); ++i) {
if (str[i - 1] == 'U' && str[i] == 'R') {
ans--;
i++;
} else if (str[i - 1] == 'R' && str[i] == 'U') {
ans--;
i++;
}
}
printf("%d\n", ans);
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import re
import sys
exit=sys.exit
from bisect import bisect_left as bsl,bisect_right as bsr
from collections import Counter,defaultdict as ddict,deque
from functools import lru_cache
cache=lru_cache(None)
from heapq import *
from itertools import *
from math import inf
from pprint import pprint as pp
enum=enumerate
ri=lambda:int(rln())
ris=lambda:list(map(int,rfs()))
rln=sys.stdin.readline
rl=lambda:rln().rstrip('\n')
rfs=lambda:rln().split()
cat=''.join
catn='\n'.join
mod=1000000007
d4=[(0,-1),(1,0),(0,1),(-1,0)]
d8=[(-1,-1),(0,-1),(1,-1),(-1,0),(1,0),(-1,1),(0,1),(1,1)]
########################################################################
n=ri()
s=rl()
ans=i=0
while i<n:
ans+=1
if i+1<n and s[i]!=s[i+1]:
i+=2
else:
i+=1
print(ans)
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int long long k, n, i, r, u;
string s;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
cin >> s;
for (i = 0; i < n - 1; i++) {
if ((s[i] == 'R' && s[i + 1] == 'U') || (s[i] == 'U' && s[i + 1] == 'R')) {
k++;
s[i + 1] = 'D';
}
}
cout << n - k;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000;
int n, m;
unordered_set<int> g[MAXN];
int color[MAXN];
int low[MAXN];
int depth[MAXN];
stack<pair<int, int> > edge_stack;
vector<pair<int, int> > edges_to_remove;
int COMP_ID;
int comp_before[MAXN];
int comp_after[MAXN];
void remove_comp(int u, int v, bool odd) {
pair<int, int> uv(u, v);
while (true) {
pair<int, int> top = edge_stack.top();
edge_stack.pop();
if (odd) edges_to_remove.push_back(top);
if (top == uv) break;
}
}
bool dfs_before(int u, int p, int d) {
comp_before[u] = COMP_ID;
depth[u] = d;
color[u] = d % 2;
low[u] = d - 1;
bool u_odd = false;
for (int v : g[u]) {
if (v == p) continue;
if (depth[v] == -1) {
edge_stack.emplace(u, v);
bool v_odd = dfs_before(v, u, d + 1);
if (p == -1) {
assert(low[v] >= d);
}
if (low[v] >= d) {
remove_comp(u, v, v_odd);
} else {
u_odd = u_odd or v_odd;
low[u] = min(low[u], low[v]);
}
} else if (depth[v] < d) {
assert(d - depth[v] >= 2);
edge_stack.emplace(u, v);
low[u] = min(low[u], depth[v]);
if (color[u] == color[v]) {
u_odd = true;
}
} else {
}
}
return u_odd;
}
void dfs_after(int u, int p) {
comp_after[u] = COMP_ID;
for (int v : g[u]) {
if (v == p or comp_after[v] != -1) continue;
dfs_after(v, u);
}
}
int main() {
scanf("%d%d", &n, &m);
while (m--) {
int a, b;
scanf("%d%d", &a, &b);
--a, --b;
g[a].insert(b);
g[b].insert(a);
}
memset(depth, -1, sizeof(depth[0]) * n);
COMP_ID = 0;
for (int u = 0; u <= n - 1; ++u) {
if (depth[u] == -1) {
dfs_before(u, -1, 0);
COMP_ID++;
}
}
for (auto& e : edges_to_remove) {
g[e.first].erase(e.second);
g[e.second].erase(e.first);
}
memset(comp_after, -1, sizeof(comp_after[0]) * n);
for (int u = 0; u <= n - 1; ++u) {
if (comp_after[u] == -1) {
dfs_after(u, -1);
COMP_ID++;
}
}
int q;
scanf("%d", &q);
while (q--) {
int a, b;
scanf("%d%d", &a, &b);
--a, --b;
if (comp_before[a] != comp_before[b])
puts("No");
else if (comp_after[a] != comp_after[b])
puts("Yes");
else if (color[a] != color[b])
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int INF = 0x3f3f3f3f;
int n, low[200000 + 1], m, fa[200000 + 1][19], cnt, root[200000 + 1], R,
depth[200000 + 1];
vector<int> g[200000 + 1], g2[200000 + 1];
vector<int> roots;
bool vis[200000 + 10], can[200000 + 20];
bool odd[200000 + 10];
int sum[200000 + 10];
stack<int> st;
void dfs(int now, int pre = 0, int deep = 1) {
low[now] = depth[now] = deep;
vis[now] = 1;
root[now] = R;
st.push(now);
for (auto it : g[now]) {
if (it == pre) continue;
if (vis[it]) {
low[now] = min(low[now], depth[it]);
} else {
dfs(it, now, deep + 1);
low[now] = min(low[now], low[it]);
if (low[it] >= depth[now]) {
int is;
++cnt;
do {
is = st.top();
st.pop();
g2[cnt].push_back(is);
g2[is].push_back(cnt);
} while (is != it);
g2[now].push_back(cnt);
g2[cnt].push_back(now);
}
}
}
}
int colo[200000 + 10];
bool try_paint(int now, int col = 1) {
vis[now] = 1;
colo[now] = col;
for (auto it : g[now]) {
if (!can[it]) continue;
if (vis[it]) {
if (col == colo[it]) {
return 0;
}
} else {
if (!try_paint(it, col ^ 1)) return 0;
}
}
return 1;
}
void dfs2(int now, int deep = 1) {
vis[now] = 1;
depth[now] = deep;
for (auto it : g[now]) {
if (!vis[it]) {
dfs2(it, deep + 1);
}
}
}
void dfs3(int now, int pre = 0, int deep = 1) {
depth[now] = deep + 1;
fa[now][0] = pre;
for (int i = 1; i <= 18; ++i) fa[now][i] = fa[fa[now][i - 1]][i - 1];
sum[now] += odd[now];
for (auto it : g2[now]) {
if (it != pre) {
sum[it] += sum[now];
dfs3(it, now, deep + 1);
}
}
}
int lca(int u, int v) {
if (depth[u] > depth[v]) swap(u, v);
int jump = depth[v] - depth[u];
for (int i = 0; i < 19; ++i) {
if ((jump >> i) & 1) {
v = fa[v][i];
}
}
if (u == v) return v;
for (int i = 18; i >= 0; --i) {
if (fa[u][i] != fa[v][i]) {
u = fa[u][i];
v = fa[v][i];
}
}
return fa[u][0];
}
pair<int, int> query[200000 + 2];
bool rest[200000 + 2];
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
cnt = n;
for (int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
R = i;
roots.push_back(i);
while (!st.empty()) st.pop();
dfs(i);
}
}
memset(vis, 0, sizeof(vis));
for (int i = n + 1; i <= cnt; ++i) {
for (auto it : g2[i]) {
can[it] = 1;
}
odd[i] = !try_paint(g2[i][0]);
for (auto it : g2[i]) {
can[it] = 0;
vis[it] = 0;
}
}
for (auto it : roots) {
dfs2(it);
}
int qqq;
cin >> qqq;
for (int i = 1; i <= qqq; ++i) {
cin >> query[i].first >> query[i].second;
if (root[query[i].first] == root[query[i].second])
if ((depth[query[i].first] + depth[query[i].second]) & 1) {
rest[i] = 1;
}
}
for (auto it : roots) {
dfs3(it);
}
for (int i = 1; i <= qqq; ++i) {
int a, b;
a = query[i].first;
b = query[i].second;
if (root[a] != root[b]) continue;
int lcc = lca(a, b);
lcc = fa[lcc][0];
if (sum[a] + sum[b] - 2 * sum[lcc]) {
rest[i] = 1;
}
}
for (int i = 1; i <= qqq; ++i) {
if (query[i].first == query[i].second) rest[i] = 0;
cout << (rest[i] ? "Yes\n" : "No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const double pi = 3.14159265358979323846;
struct UnionFind {
vector<int> p, rank;
void assign(int N) {
rank.assign(N, 0);
p.resize(N);
for (int i = 0; i < N; ++i) p[i] = i;
}
UnionFind(int N) {
rank.assign(N, 0);
p.resize(N);
for (int i = 0; i < N; ++i) p[i] = i;
}
int findSet(int i) { return (p[i] == i ? i : (p[i] = findSet(p[i]))); }
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
void unionSet(int i, int j) {
if (!isSameSet(i, j)) {
int x = findSet(i), y = findSet(j);
if (rank[x] > rank[y])
p[y] = x;
else {
p[x] = y;
if (rank[x] == rank[y]) rank[y]++;
}
}
}
};
int n, m;
vector<int> dfs_low, dfs_level;
vector<vector<int>> graph;
stack<pair<int, int>> S;
unordered_map<int, unordered_map<int, bool>> dirty;
bool tarjanBCC(int u, int level, int parent) {
dfs_low[u] = dfs_level[u] = level;
bool dirt = false;
for (int v : graph[u]) {
if (parent == v) continue;
if (dfs_level[v] == -1) {
S.emplace(u, v);
bool aux = tarjanBCC(v, level + 1, u);
dfs_low[u] = min(dfs_low[u], dfs_low[v]);
if (parent == -1 || (parent != -1 && dfs_low[v] >= dfs_level[u])) {
while (true) {
pair<int, int> P = S.top();
S.pop();
if (aux) dirty[P.first][P.second] = dirty[P.second][P.first] = true;
if (P == pair<int, int>(u, v)) break;
}
} else
dirt = aux || dirt;
} else if (dfs_level[v] < dfs_level[u]) {
S.emplace(u, v);
dfs_low[u] = min(dfs_low[u], dfs_level[v]);
if ((dfs_level[u] - dfs_level[v]) % 2 == 0) dirt = true;
}
}
return dirt;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
UnionFind Connected(n);
graph.resize(n);
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
graph[--a].push_back(--b);
graph[b].push_back(a);
Connected.unionSet(a, b);
}
dfs_low.assign(n, -1);
dfs_level.assign(n, -1);
for (int u = 0; u < n; ++u)
if (dfs_level[u] == -1) tarjanBCC(u, 0, -1);
vector<int> color(n, -1);
UnionFind U(n);
for (int node = 0; node < n; ++node)
if (color[node] == -1) {
color[node] = 0;
queue<int> q;
q.push(node);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : graph[u])
if (color[v] == -1 && !dirty[u][v]) {
color[v] = (color[u] == 0 ? 1 : 0);
U.unionSet(u, v);
q.push(v);
}
}
}
int Q;
cin >> Q;
while (Q--) {
int a, b;
cin >> a >> b;
--a;
--b;
if (a == b || !Connected.isSameSet(a, b))
cout << "No\n";
else if (!U.isSameSet(a, b))
cout << "Yes\n";
else
cout << (color[a] != color[b] ? "Yes\n" : "No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, lgn, m, q, f[N][20], d[N], cov[N], b, bel[N];
vector<int> G[N], T[N];
int lca(int u, int v) {
if (d[u] < d[v]) swap(u, v);
int c = d[u] - d[v];
for (int i = lgn - 1; ~i; i--)
if (c >> i & 1) u = f[u][i];
if (u == v) return u;
for (int i = lgn - 1; ~i; i--)
if (f[u][i] ^ f[v][i]) {
u = f[u][i];
v = f[v][i];
}
return f[u][0];
}
void dfs(int u, int fa = 0) {
d[u] = d[fa] + 1;
f[u][0] = fa;
bel[u] = b;
for (int i = 1; i < lgn; i++) {
f[u][i] = f[f[u][i - 1]][i - 1];
if (!f[u][i]) break;
}
for (int v : G[u])
if (!bel[v]) {
dfs(v, u);
T[u].push_back(v);
}
}
pair<int, int> stk[N];
int top, low[N], dfn[N], idx;
void tarjan(int u, int fa = 0) {
low[u] = dfn[u] = ++idx;
for (int v : G[u]) {
if (!dfn[v]) {
stk[++top] = pair<int, int>(u, v);
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
int tu = 0, tv = 0, ttop = top;
bool tag = 0;
do {
tu = stk[top].first;
tv = stk[top].second;
top--;
if ((d[tu] + d[tv]) % 2 == 0) {
tag = 1;
break;
}
} while (!(tu == u && tv == v));
if (tag) {
top = ttop;
do {
tu = stk[top].first;
tv = stk[top].second;
top--;
cov[tu] = cov[tv] = 1;
} while (!(tu == u && tv == v));
cov[u] = 0;
}
}
} else if (dfn[v] < dfn[u] && v != fa) {
stk[++top] = pair<int, int>(v, u);
low[u] = min(low[u], dfn[v]);
}
}
}
void dfs2(int u, int fa = 0) {
cov[u] += cov[fa];
for (int v : T[u]) dfs2(v, u);
}
int main() {
scanf("%d%d", &n, &m);
for (lgn = 1; (1 << lgn) <= n; lgn++)
;
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
static bool root[N];
for (int i = 1; i <= n; i++)
if (!bel[i]) b++, root[i] = 1, dfs(i);
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i);
for (int i = 1; i <= n; i++)
if (root[i]) dfs2(i);
scanf("%d", &q);
while (q--) {
int u, v;
scanf("%d%d", &u, &v);
bool tag = 0;
if (bel[u] ^ bel[v])
tag = 0;
else if ((d[u] ^ d[v]) & 1)
tag = 1;
else
tag = cov[u] + cov[v] - 2 * cov[lca(u, v)] > 0;
puts(tag ? "Yes" : "No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int read() {
int x = 0, f = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 3) + (x << 1) + c - 48;
c = getchar();
}
return x * f;
}
int to[maxn << 1], net[maxn << 1], h[maxn], e = 0;
inline void add(int x, int y) {
to[++e] = y;
net[e] = h[x];
h[x] = e;
}
int dclock = 0, top = 0, color = 0;
int dfn[maxn], low[maxn], dep[maxn], f[maxn][21], v[maxn], odd[maxn], st[maxn],
c[maxn], s[maxn];
inline void dfs(int x, int fa) {
c[x] = color;
f[x][0] = fa;
dfn[x] = low[x] = ++dclock;
dep[x] = dep[fa] + 1;
v[x] = 1;
st[++top] = x;
for (register int i = 1; (1 << i) <= dep[x]; i++)
f[x][i] = f[f[x][i - 1]][i - 1];
for (register int i = h[x]; i; i = net[i]) {
int y = to[i];
if (y == fa) continue;
if (!dfn[y]) {
dfs(y, x);
low[x] = min(low[x], low[y]);
} else if (v[y]) {
low[x] = min(low[x], low[y]);
if (dep[x] % 2 == dep[y] % 2) odd[x] = 1;
}
}
v[x] = 0;
if (low[x] == dfn[x]) {
bool flag = 0;
for (register int i = top; i; i--) {
if (st[i] == x) break;
if (odd[st[i]]) {
flag = 1;
break;
}
}
if (flag) {
while (top && st[top] != x) {
s[st[top]]++;
v[st[top]] = 0;
top--;
}
top--;
} else {
while (top && st[top] != x) {
v[st[top]] = 0;
top--;
}
top--;
}
}
}
int LCA(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (register int i = 20; i >= 0; i--)
if (dep[f[x][i]] >= dep[y]) x = f[x][i];
if (x == y) return x;
for (register int i = 20; i >= 0; i--) {
if (f[x][i] != f[y][i]) {
x = f[x][i];
y = f[y][i];
}
}
return f[x][0];
}
inline void dfs1(int x) {
for (register int i = h[x]; i; i = net[i]) {
int y = to[i];
if (f[y][0] == x) {
s[y] += s[x];
dfs1(y);
}
}
return;
}
int k, m, n, t, root[maxn];
int main() {
n = read();
m = read();
for (register int i = 1; i <= m; i++) {
int x = read(), y = read();
add(x, y);
add(y, x);
}
for (register int i = 1; i <= n; i++) {
if (!c[i]) {
color++;
dfs(i, 0);
root[i] = 1;
}
}
for (register int i = 1; i <= n; i++) {
if (root[i]) dfs1(i);
}
t = read();
for (register int i = 1; i <= t; i++) {
int x = read(), y = read();
if (c[x] == c[y]) {
int lca = LCA(x, y);
if ((dep[x] + dep[y] - 2 * dep[lca]) % 2 || (s[x] + s[y] - 2 * s[lca])) {
puts("Yes");
continue;
}
}
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
struct DSU {
int fa[maxn];
inline void init(int n) {
for (int i = 1; i <= n; i++) fa[i] = i;
}
int findset(int x) { return fa[x] == x ? x : fa[x] = findset(fa[x]); }
} dsu;
vector<int> G[maxn];
int dep[maxn], anc[maxn][20], odd[maxn];
void dfs(int now, int fa, int depth) {
anc[now][0] = fa;
dep[now] = depth;
for (const int& to : G[now])
if (!dep[to]) {
dfs(to, now, depth + 1);
if (dsu.findset(now) == dsu.findset(to)) odd[now] |= odd[to];
} else if (dep[to] + 1 < dep[now]) {
if (!((dep[to] + dep[now]) & 1)) odd[now] = 1;
for (int u = dsu.findset(now); dep[to] + 1 < dep[u]; u = dsu.findset(u))
dsu.fa[u] = anc[u][0];
}
}
int cnt[maxn];
void dfs2(int now) {
cnt[now] += odd[now];
for (const int& to : G[now])
if (dep[to] == dep[now] + 1) {
if (dsu.findset(now) == dsu.findset(to)) odd[to] |= odd[now];
cnt[to] = cnt[now];
dfs2(to);
}
}
inline void preprocess(int n) {
dsu.init(n);
for (int i = 1; i <= n; i++)
if (!dep[i]) dfs(i, 0, 1);
for (int i = 1; i < 20; i++)
for (int now = 1; now <= n; now++)
anc[now][i] = anc[anc[now][i - 1]][i - 1];
for (int i = 1; i <= n; i++)
if (dep[i] == 1) dfs2(i);
}
inline int LCA(int a, int b) {
if (dep[a] > dep[b]) swap(a, b);
int ddep = dep[b] - dep[a];
for (int i = 0; ddep; i++, ddep >>= 1)
if (ddep & 1) b = anc[b][i];
if (a == b) return a;
for (int i = 19; i >= 0; i--)
if (anc[a][i] != anc[b][i]) a = anc[a][i], b = anc[b][i];
assert(anc[a][0] == anc[b][0]);
return anc[a][0];
}
inline bool query(int a, int b) {
int lca = LCA(a, b);
if (lca == 0) return 0;
if (((dep[a] + dep[b]) & 1) || (cnt[a] + cnt[b] - 2 * cnt[lca] > 0))
return 1;
else
return 0;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0, a, b; i < m; i++) {
scanf("%d%d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
preprocess(n);
int q;
scanf("%d", &q);
for (int i = 0, a, b; i < q; i++) {
scanf("%d%d", &a, &b);
printf("%s\n", query(a, b) ? "Yes" : "No");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
struct Dj {
unordered_map<int, int> a;
int find(int x) { return a.count(x) ? (a[x] = find(a[x])) : x; }
void merge(int x, int y) {
x = find(x);
y = find(y);
if (x != y) a[x] = y;
}
bool same(int x, int y) {
x = find(x);
y = find(y);
return x == y;
}
};
Dj naive;
vector<int> adj[100000];
void add_edge(int a, int b) {
naive.merge(2 * a, 2 * b + 1);
naive.merge(2 * a + 1, 2 * b);
adj[a].push_back(b);
adj[b].push_back(a);
}
int depth[100000];
stack<int> torjanstack;
int n2;
vector<int> adj2[200000];
bool isstar[200000];
void handle_bcc(const vector<int> &bcc) {
Dj dj;
for (int x : bcc)
for (int c : adj[x]) {
dj.merge(2 * x, 2 * c + 1);
dj.merge(2 * c, 2 * x + 1);
}
int ref = bcc.front();
if (dj.same(2 * ref, 2 * ref + 1)) {
int star = n2++;
isstar[star] = true;
for (int x : bcc) {
adj2[x].push_back(star);
adj2[star].push_back(x);
}
} else {
int even = n2++;
int odd = n2++;
adj2[even].push_back(odd);
adj2[odd].push_back(even);
for (int x : bcc) {
int y = dj.same(2 * ref, 2 * x) ? even : odd;
adj2[x].push_back(y);
adj2[y].push_back(x);
}
}
}
int torjan(int x, int p) {
depth[x] = p == -1 ? 1 : depth[p] + 1;
int mind = depth[x];
torjanstack.push(x);
for (int c : adj[x]) {
if (c == p) continue;
if (depth[c]) {
mind = min(mind, depth[c]);
continue;
}
int cd = torjan(c, x);
if (cd > depth[x]) {
adj2[x].push_back(c);
adj2[c].push_back(x);
continue;
}
if (cd < depth[x]) {
mind = min(mind, cd);
continue;
}
vector<int> bcc;
int y;
do {
y = torjanstack.top();
torjanstack.pop();
bcc.push_back(y);
} while (y != c);
bcc.push_back(x);
handle_bcc(bcc);
}
if (mind == depth[x]) torjanstack.pop();
return mind;
}
int depth2[200000];
int anc[200000][18];
bool hasstar[200000][18];
void dfs(int x, int p) {
if (p == -1) {
anc[x][0] = x;
depth2[x] = 1;
} else {
anc[x][0] = p;
depth2[x] = depth2[p] + 1;
}
for (int c : adj2[x])
if (c != p) dfs(c, x);
}
void preprocess(int n) {
n2 = n;
for (int i = 0; i < n; i++)
if (depth[i] == 0) torjan(i, -1);
for (int i = 0; i < n2; i++)
if (depth2[i] == 0) dfs(i, -1);
for (int i = 0; i < n2; i++) hasstar[i][0] = isstar[i];
for (int i = 1; i < 18; i++)
for (int j = 0; j < n2; j++) {
int m = anc[j][i - 1];
anc[j][i] = anc[m][i - 1];
hasstar[j][i] = hasstar[j][i - 1] || hasstar[m][i - 1];
}
}
bool query(int a, int b) {
if (a == b || naive.find(2 * a) != naive.find(2 * b + 1)) return false;
if ((depth2[a] + depth2[b]) % 2) return true;
if (depth2[a] < depth2[b]) swap(a, b);
for (int i = 0; i < 18; i++)
if ((depth2[a] - depth2[b]) & (1 << i)) {
if (hasstar[a][i]) return true;
a = anc[a][i];
}
if (a == b) return false;
for (int i = 18 - 1; i >= 0; i--) {
if (anc[a][i] != anc[b][i]) {
if (hasstar[a][i] || hasstar[b][i]) return true;
a = anc[a][i];
b = anc[b][i];
}
}
return isstar[a] || isstar[b] || isstar[anc[a][0]];
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d %d", &a, &b);
a--, b--;
add_edge(a, b);
}
preprocess(n);
int q;
scanf("%d", &q);
for (int i = 0; i < q; i++) {
int a, b;
scanf("%d %d", &a, &b);
a--, b--;
puts(query(a, b) ? "Yes" : "No");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
vector<int> comp_before;
vector<int> comp_after;
vector<int> low;
vector<int> depth;
vector<int> color;
vector<set<int>> g;
stack<pair<int, int>> s;
vector<pair<int, int>> banned;
int curr_comp;
int n, m;
void removeComp(bool odd, pair<int, int> e) {
while (true) {
if (odd) banned.push_back(s.top());
if (s.top() == e) {
s.pop();
break;
} else
s.pop();
}
}
bool dfs(int u, int p, int d) {
comp_before[u] = curr_comp;
depth[u] = d;
low[u] = d;
bool odd = false;
for (int v : g[u]) {
if (v == p) continue;
if (depth[v] == -1) {
pair<int, int> uv(u, v);
s.push(uv);
bool v_odd = dfs(v, u, d + 1);
if ((p == -1) || (low[v] >= depth[u]))
removeComp(v_odd, uv);
else {
odd = odd or v_odd;
low[u] = min(low[u], low[v]);
}
} else if (depth[v] < depth[u]) {
if ((depth[u] - depth[v]) % 2 == 0) odd = true;
s.push({u, v});
low[u] = min(low[u], depth[v]);
}
}
return odd;
}
void bfs(int s) {
queue<int> q;
q.push(s);
color[s] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
comp_after[u] = curr_comp;
for (int v : g[u]) {
if (color[v] == -1) {
color[v] = 1 - color[u];
q.push(v);
}
}
}
}
int main() {
scanf("%d%d", &n, &m);
g.assign(n, set<int>());
comp_before.resize(n);
comp_after.assign(n, -1);
low.resize(n);
depth.assign(n, -1);
color.assign(n, -1);
banned.clear();
while (!s.empty()) s.pop();
for (int i = 1; i <= m; ++i) {
int a, b;
scanf("%d%d", &a, &b), --a, --b;
g[a].insert(b);
g[b].insert(a);
}
curr_comp = 0;
for (int i = 0; i <= n - 1; ++i)
if (depth[i] == -1) dfs(i, -1, 0), curr_comp++;
for (auto e : banned) {
g[e.first].erase(e.second);
g[e.second].erase(e.first);
}
curr_comp = 0;
for (int i = 0; i <= n - 1; ++i)
if (color[i] == -1) bfs(i), curr_comp++;
int q;
scanf("%d", &q);
while (q--) {
int a, b;
scanf("%d%d", &a, &b), --a, --b;
if (comp_before[a] != comp_before[b])
puts("No");
else if (comp_after[a] != comp_after[b])
puts("Yes");
else if (color[a] != color[b])
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
const int N = 1e5 + 2;
int n, m, q, cnf, col[N], bel[N << 1];
bool eve[N << 1];
template <class T>
inline void apn(T &x, const T y) {
if (x > y) x = y;
}
inline char get_c() {
static char *h, *t, buf[200000];
if (h == t) {
t = (h = buf) + fread(buf, 1, 200000, stdin);
if (h == t) return EOF;
}
return *h++;
}
inline int nxi() {
int x = 0;
char c;
while ((c = get_c()) > '9' || c < '0')
;
while (x = x * 10 - 48 + c, (c = get_c()) >= '0' && c <= '9')
;
return x;
}
namespace G {
int cnt = 1, fir[N], dfn[N], low[N];
bool vis[N << 1];
struct edge {
int fr, to, nx;
} eg[N << 1];
inline void add_edge(const int a, const int b) {
eg[++cnt] = (edge){a, b, fir[a]};
fir[a] = cnt;
}
void tarjan(const int x) {
static int cnd, top, stk[N << 1];
low[x] = dfn[x] = ++cnd;
for (int i = fir[x]; i; i = eg[i].nx) {
const int y = eg[i].to;
if (!vis[i]) stk[++top] = i;
if (dfn[y]) {
apn(low[x], dfn[y]);
} else {
tarjan(y);
if (low[y] >= dfn[x]) {
int j = 0;
++cnf;
while (j != i) {
j = stk[top--];
bel[j] = bel[j ^ 1] = cnf;
vis[j] = vis[j ^ 1] = 1;
}
} else
apn(low[x], low[y]);
}
}
}
void paint(const int x) {
for (int i = fir[x]; i; i = eg[i].nx) {
const int y = eg[i].to;
if (~col[y]) {
if (col[y] == col[x]) eve[bel[i]] = 1;
} else {
col[y] = col[x] ^ 1;
paint(y);
}
}
}
} // namespace G
namespace T {
int dep[N], rt[N], fa[18][N];
bool exi[18][N], vis[N];
void dfs(const int x) {
using G::eg;
using G::fir;
vis[x] = 1;
for (int i = 1; i < 18 && fa[i - 1][x]; ++i) {
fa[i][x] = fa[i - 1][fa[i - 1][x]];
}
for (int i = fir[x]; i; i = eg[i].nx) {
const int y = eg[i].to;
if (!vis[y]) {
fa[0][y] = x;
dep[y] = dep[x] + 1;
rt[y] = rt[x];
dfs(y);
}
}
}
void build() {
for (int i = 1; i < 18; ++i) {
for (int j = 1; j <= n; ++j) {
exi[i][j] = exi[i - 1][j] | exi[i - 1][fa[i - 1][j]];
}
}
}
inline int lca(int x, int y) {
if (dep[x] < dep[y]) std::swap(x, y);
int dis = dep[x] - dep[y];
for (int i = 0; i < 18; ++i) {
if (dis & (1 << i)) x = fa[i][x];
}
if (x == y) return x;
for (int i = 17; i >= 0; --i) {
if (fa[i][x] != fa[i][y]) {
x = fa[i][x];
y = fa[i][y];
}
}
return fa[0][x];
}
inline bool get_exi(int x, int f) {
bool res = 0;
int dis = dep[x] - dep[f];
for (int i = 17; i >= 0 && !res; --i) {
if (dis & (1 << i)) {
res |= exi[i][x];
x = fa[i][x];
}
}
return res;
}
} // namespace T
int main() {
memset(col, -1, sizeof(col));
n = nxi(), m = nxi();
for (int i = 1; i <= m; ++i) {
const int a = nxi(), b = nxi();
G::add_edge(a, b);
G::add_edge(b, a);
}
for (int i = 1; i <= n; ++i) {
if (!G::dfn[i]) G::tarjan(i);
}
for (int i = 1; i <= n; ++i) {
if (col[i] == -1) col[i] = 0, G::paint(i);
}
for (int i = 1; i <= n; ++i) {
if (!T::dep[i]) T::rt[i] = i, T::dfs(i);
}
for (int i = 2; i <= G::cnt; ++i) {
using G::eg;
using T::fa;
if (eve[bel[i]]) {
const int x = eg[i].fr, y = eg[i].to;
if (fa[0][x] != y && fa[0][y] != x) continue;
T::exi[0][fa[0][x] == y ? x : y] = 1;
}
}
T::build();
q = nxi();
while (q--) {
const int x = nxi(), y = nxi();
if (T::rt[x] != T::rt[y]) {
puts("No");
continue;
}
const int z = T::lca(x, y);
if ((T::dep[x] + T::dep[y] - (T::dep[z] << 1)) & 1)
puts("Yes");
else
puts(T::get_exi(x, z) || T::get_exi(y, z) ? "Yes" : "No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using std ::cerr;
using std ::map;
using std ::max;
using std ::min;
using std ::queue;
using std ::set;
using std ::sort;
using std ::vector;
class Input {
private:
char buf[1000000], *p1 = buf, *p2 = buf;
inline char gc() {
if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin);
return p1 == p2 ? EOF : *(p1++);
}
public:
Input() {}
template <typename T>
inline Input &operator>>(T &x) {
x = 0;
int f = 1;
char a = gc();
for (; !isdigit(a); a = gc())
if (a == '-') f = -1;
for (; isdigit(a); a = gc()) x = x * 10 + a - '0';
x *= f;
return *this;
}
inline Input &operator>>(char *s) {
int p = 0;
while (1) {
s[p] = gc();
if (s[p] == '\n' || s[p] == ' ') break;
p++;
}
s[p] = '\0';
return *this;
}
} Fin;
class Output {
private:
char ouf[1000000], *p1 = ouf, *p2 = ouf;
char Of[105], *o1 = Of, *o2 = Of;
void flush() {
fwrite(ouf, 1, p2 - p1, stdout);
p2 = p1;
}
inline void pc(char ch) {
*(p2++) = ch;
if (p2 == p1 + 1000000) flush();
}
public:
template <typename T>
inline Output &operator<<(T n) {
if (n < 0) pc('-'), n = -n;
if (n == 0) pc('0');
while (n) *(o1++) = (n % 10) ^ 48, n /= 10;
while (o1 != o2) pc(*(--o1));
return *this;
}
inline Output &operator<<(const char ch) {
pc(ch);
return *this;
}
inline Output &operator<<(const char *ch) {
int p = 0;
while (ch[p] != '\0') {
pc(ch[p]);
p++;
}
return *this;
}
~Output() { flush(); }
} Fout;
const int N = 2e5 + 10;
int n, m;
int lg[N], dep[N], fa[N][20], Fa[N];
inline int find(int x) { return x == Fa[x] ? x : Fa[x] = find(Fa[x]); }
struct Graph {
struct edge {
int to, next;
} e[N << 1];
int cnt, head[N];
void push(int x, int y) {
e[++cnt] = (edge){y, head[x]};
head[x] = cnt;
}
} Gr[2];
Graph *T = &Gr[0], *G = &Gr[1];
int Dfn[N];
void dfs1(int x, int fx) {
dep[x] = dep[fx] + 1;
fa[x][0] = fx;
Dfn[x] = ++Dfn[0];
for (int i = 1; i <= lg[dep[x]]; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1];
for (int i = T->head[x]; i; i = T->e[i].next) {
int y = T->e[i].to;
if (y == fx) continue;
dfs1(y, x);
}
}
int lca(int x, int y) {
if (dep[x] < dep[y]) std ::swap(x, y);
while (dep[x] > dep[y]) x = fa[x][lg[dep[x] - dep[y]]];
if (x == y) return x;
for (int i = lg[dep[x]]; i >= 0; i--)
if (fa[x][i] ^ fa[y][i]) x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
int dfn[N], low[N], stk[N], top;
int mark[N], cc[N];
long long tg[N];
inline bool cmp(int x, int y) { return Dfn[x] <= Dfn[y]; }
int Flag;
void pd(int x, int c) {
mark[x] = -1;
cc[x] = c;
for (int i = G->head[x]; i; i = G->e[i].next) {
int y = G->e[i].to;
if (mark[y] == 1)
pd(y, c ^ 1);
else if (mark[y] == -1) {
if (cc[y] == c) {
Flag = 1;
return;
} else
continue;
}
}
}
void tarjan(int x, int las) {
stk[++top] = x;
dfn[x] = low[x] = ++dfn[0];
for (int i = G->head[x]; i; i = G->e[i].next) {
int y = G->e[i].to;
if (y == las) continue;
if (!dfn[y]) {
tarjan(y, x);
low[x] = min(low[x], low[y]);
if (low[y] >= dfn[x]) {
vector<int> ver;
ver.push_back(x);
while (1) {
int u = stk[top--];
ver.push_back(u);
if (u == y) break;
}
for (int p : ver) mark[p] = 1;
Flag = 0;
pd(x, 0);
for (int p : ver) mark[p] = 0;
if (Flag) {
sort(ver.begin(), ver.end(), cmp);
for (int j = 0; j < ver.size() - 1; j++) {
int lc = lca(ver[j], ver[j + 1]);
tg[ver[j]]++;
tg[ver[j + 1]]++;
tg[lc] -= 2;
}
int a = *ver.begin();
int b = *(--ver.end());
int lc = lca(a, b);
tg[a]++;
tg[b]++;
tg[lc] -= 2;
}
}
} else
low[x] = min(low[x], dfn[y]);
}
}
void dfs2(int x, int fx) {
mark[x] = 1;
for (int i = T->head[x]; i; i = T->e[i].next) {
int y = T->e[i].to;
if (y == fx) continue;
dfs2(y, x);
tg[x] += tg[y];
}
}
long long sum[N];
void dfs3(int x, int fx, long long s) {
sum[x] = s;
for (int i = T->head[x]; i; i = T->e[i].next) {
int y = T->e[i].to;
if (y == fx) continue;
dfs3(y, x, s + tg[y]);
}
}
bool check(int x, int y) {
if (find(x) != find(y)) return 0;
int lc = lca(x, y);
if ((dep[x] + dep[y] - dep[lc] * 2) % 2 == 1) return 1;
return sum[x] + sum[y] - sum[lc] * 2 != 0;
}
int main() {
Fin >> n >> m;
for (int i = 1; i <= n; i++) Fa[i] = i;
for (int i = 1; i <= m; i++) {
int x, y;
Fin >> x >> y;
G->push(x, y);
G->push(y, x);
if (find(x) != find(y)) {
T->push(x, y), T->push(y, x), Fa[find(x)] = find(y);
}
}
for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
for (int i = 1; i <= n; i++)
if (!dep[i]) dfs1(i, 0);
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i, 0);
for (int i = 1; i <= n; i++) {
if (!mark[i]) {
dfs2(i, 0);
dfs3(i, 0, tg[i]);
}
}
int Q;
Fin >> Q;
while (Q--) {
int x, y;
Fin >> x >> y;
if (check(x, y))
Fout << "Yes" << '\n';
else
Fout << "No" << '\n';
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
template <typename T>
inline bool chkmax(T &a, T b) {
return a < b ? a = b, true : false;
}
template <typename T>
inline bool chkmin(T &a, T b) {
return a > b ? a = b, true : false;
}
template <typename T>
void read(T &first) {
int f = 1;
char ch;
for (ch = getchar(); !isdigit(ch); ch = getchar()) {
if (ch == '-') f = -1;
}
for (first = 0; isdigit(ch); ch = getchar()) {
first = first * 10 + ch - '0';
}
first *= f;
}
const int MAXN = 1e5 + 5, MAXM = 1e5 + 5;
struct Edge {
int v, next;
Edge() {}
Edge(int v_, int next_) : v(v_), next(next_) {}
};
int N, M, Q;
int tote, head[MAXN];
Edge edge[MAXM * 2 + 5];
int fa[MAXN], dep[MAXN], size[MAXN], hson[MAXN], top[MAXN];
int curtid, tid[MAXN];
int sum[MAXN];
int dfsclock, dfn[MAXN], low[MAXN];
std::pair<int, int> st[MAXN];
int sttop;
inline void addEdge(int u, int v) {
edge[++tote] = Edge(v, head[u]);
head[u] = tote;
}
void dfs1(int u) {
tid[u] = curtid;
size[u] = 1;
for (int i = head[u]; i; i = edge[i].next) {
int v = edge[i].v;
if (!tid[v]) {
fa[v] = u;
dep[v] = dep[u] + 1;
dfs1(v);
size[u] += size[v];
if (size[v] > size[hson[u]]) hson[u] = v;
}
}
}
void dfs2(int u) {
if (hson[u]) {
top[hson[u]] = top[u];
dfs2(hson[u]);
for (int i = head[u]; i; i = edge[i].next) {
int v = edge[i].v;
if (fa[v] == u && v != hson[u]) {
top[v] = v;
dfs2(v);
}
}
}
}
inline long long getLCA(int u, int v) {
while (top[u] != top[v]) {
if (dep[top[u]] > dep[top[v]])
u = fa[top[u]];
else
v = fa[top[v]];
}
return dep[u] > dep[v] ? v : u;
}
void tarjan(int u) {
dfn[u] = low[u] = ++dfsclock;
for (int i = head[u]; i; i = edge[i].next) {
int v = edge[i].v;
std::pair<int, int> cur = std::make_pair(u, v);
if (fa[v] == u) {
st[++sttop] = cur;
tarjan(v);
chkmin(low[u], low[v]);
if (low[v] >= dfn[u]) {
int f = 0;
for (int j = sttop;; --j) {
if ((dep[st[j].first] & 1) == (dep[st[j].second] & 1)) {
f = 1;
break;
}
if (st[j] == cur) break;
}
do {
if (st[sttop].first != u) sum[st[sttop].first] = f;
if (st[sttop].second != u) sum[st[sttop].second] = f;
} while (st[sttop--] != cur);
}
} else if (dfn[v] < dfn[u] && fa[u] != v) {
chkmin(low[u], dfn[v]);
st[++sttop] = cur;
}
}
}
void getsum(int u) {
sum[u] += sum[fa[u]];
for (int i = head[u]; i; i = edge[i].next) {
int v = edge[i].v;
if (fa[v] == u) getsum(v);
}
}
bool haveOddPath(int u, int v) {
if (tid[u] != tid[v]) return false;
if ((dep[u] & 1) != (dep[v] & 1)) return true;
int lca = getLCA(u, v);
return sum[u] + sum[v] - (sum[lca] << 1) > 0;
}
void input() {
read(N);
read(M);
for (int i = 1; i <= M; ++i) {
int u, v;
read(u);
read(v);
addEdge(u, v);
addEdge(v, u);
}
read(Q);
}
void solve() {
for (int i = 1; i <= N; ++i) {
if (!tid[i]) {
++curtid;
dep[i] = 1;
dfs1(i);
top[i] = i;
dfs2(i);
dfsclock = 0;
tarjan(i);
getsum(i);
}
}
while (Q--) {
int u, v;
read(u);
read(v);
puts(haveOddPath(u, v) ? "Yes" : "No");
}
}
int main() {
input();
solve();
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int get() {
char ch;
int s = 0;
bool bz = 0;
while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-')
;
if (ch == '-')
bz = 1;
else
s = ch - '0';
while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
if (bz) return -s;
return s;
}
const int N = 1e+5 + 10;
struct edge {
int x, nxt, id;
} e[N * 2];
int h[N], tot;
int s[N], fa[N][20], n, m, q, dep[N];
int dfn[N], f[N], k, sta[N], top, tim, b[N], te;
bool pd[N], bz[N];
void inse(int x, int y, int id) {
e[++tot].x = y;
e[tot].nxt = h[x];
e[tot].id = id;
h[x] = tot;
}
void tarjan(int x, int co) {
dfn[x] = f[x] = ++k;
sta[++top] = x;
pd[x] = 1;
for (int i = 1; i <= tim; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1];
for (int p = h[x]; p; p = e[p].nxt)
if (e[p].id != co) {
if (!dfn[e[p].x]) {
fa[e[p].x][0] = x;
dep[e[p].x] = dep[x] + 1;
tarjan(e[p].x, e[p].id);
f[x] = min(f[x], f[e[p].x]);
} else if (pd[e[p].x]) {
f[x] = min(f[x], dfn[e[p].x]);
if (dep[e[p].x] % 2 == dep[x] % 2) bz[x] = 1;
}
}
pd[x] = 0;
if (dfn[x] == f[x]) {
int w = top;
bool v = 0;
while (sta[w] != x) v |= bz[sta[w--]];
if (v)
for (int i = w + 1; i <= top; i++) s[sta[i]]++;
te++;
for (int i = w; i <= top; i++) {
b[sta[i]] = te;
pd[sta[i]] = 0;
}
top = w - 1;
}
}
int getfather(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = tim; i >= 0; i--)
if (dep[fa[x][i]] >= dep[y]) x = fa[x][i];
if (x == y) return x;
for (int i = tim; i >= 0; i--)
if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
void dfs(int x) {
for (int p = h[x]; p; p = e[p].nxt)
if (fa[e[p].x][0] == x) {
s[e[p].x] += s[x];
dfs(e[p].x);
}
}
int main() {
n = get();
m = get();
for (int i = 1; i <= m; i++) {
int x = get(), y = get();
inse(x, y, i);
inse(y, x, i);
}
tim = log(n) / log(2) + 1;
for (int i = 1; i <= n; i++)
if (!dfn[i]) {
dep[i] = 1;
fa[i][0] = i;
tarjan(i, 0);
}
for (int i = 1; i <= n; i++)
if (fa[i][0] == i) dfs(i);
q = get();
for (int i = 1; i <= q; i++) {
int x = get(), y = get();
if (fa[x][tim] != fa[y][tim]) {
printf("No\n");
continue;
}
int z = getfather(x, y);
if ((dep[x] + dep[y] - dep[z] * 2) % 2 == 1 || s[x] + s[y] - s[z] * 2 > 0)
printf("Yes\n");
else
printf("No\n");
}
fclose(stdin);
fclose(stdout);
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int Inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3fll;
struct DSU {
int fa[1 << 17];
DSU() {
for (int i = 0; i < (1 << 17); i++) fa[i] = i;
}
int inline root(int x) { return fa[x] == x ? x : (fa[x] = root(fa[x])); }
} dsu;
int n, m, q;
vector<int> nei[1 << 17];
int ceng[1 << 17];
int fa[20][1 << 17];
int hv[1 << 17], qzh[1 << 17];
void inline dfs(int now) {
ceng[now] = ceng[fa[0][now]] + 1;
for (__typeof((nei[now]).begin()) i = (nei[now]).begin(),
_e_D_ = (nei[now]).end();
i != _e_D_; i++) {
if (!ceng[*i]) {
fa[0][*i] = now;
dfs(*i);
if (dsu.root(*i) == dsu.root(now)) hv[now] |= hv[*i];
} else if (ceng[*i] + 1 < ceng[now]) {
if (!((ceng[now] ^ ceng[*i]) & 1)) hv[now] = 1;
for (int j = dsu.root(now); ceng[j] > ceng[*i] + 1; j = dsu.root(j))
dsu.fa[j] = fa[0][j];
}
}
}
void inline dfs2(int now) {
qzh[now] += hv[now];
for (__typeof((nei[now]).begin()) i = (nei[now]).begin(),
_e_D_ = (nei[now]).end();
i != _e_D_; i++)
if (ceng[*i] == ceng[now] + 1) {
if (dsu.root(*i) == dsu.root(now)) hv[*i] |= hv[now];
qzh[*i] = qzh[now];
dfs2(*i);
}
}
int inline lca(int a, int b) {
if (ceng[a] > ceng[b]) swap(a, b);
int toup = ceng[b] - ceng[a];
while (toup) b = fa[__builtin_ctz(toup)][b], toup &= (toup - 1);
if (a == b) return a;
for (int i = (19); ((-1) > 0 ? i <= (0) : i >= (0)); i += (-1))
if (fa[i][a] != fa[i][b]) a = fa[i][a], b = fa[i][b];
return fa[0][a];
}
bool inline query(int a, int b) {
int c = lca(a, b);
if (!c) return 0;
if ((ceng[a] ^ ceng[b]) & 1)
return 1;
else
return qzh[a] + qzh[b] - 2 * qzh[c];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= (m); i++) {
int a, b;
scanf("%d%d", &a, &b);
nei[a].push_back(b);
nei[b].push_back(a);
}
for (int i = 1; i <= (n); i++)
if (!ceng[i]) dfs(i), dfs2(i);
for (int i = 1; i <= (19); i++)
for (int j = 1; j <= (n); j++) fa[i][j] = fa[i - 1][fa[i - 1][j]];
scanf("%d", &q);
while (q--) {
int a, b;
scanf("%d%d", &a, &b);
puts(query(a, b) ? "Yes" : "No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
namespace FastIO {
const int L = 1 << 15;
char buffer[L], *S, *T;
inline int read() {
register char c;
register int rec = 0, f = 1;
for (c = getchar(); c < '0' || c > '9'; c = getchar())
if (c == '-') f = -1;
while (c >= '0' && c <= '9')
rec = (rec << 1) + (rec << 3) + (c - '0'), c = getchar();
return rec * f;
}
} // namespace FastIO
using FastIO::read;
const int maxn = 100005;
struct Edge {
int to, next;
} a[maxn * 2];
int h[maxn], cnt;
int n, m, q;
int prt[maxn];
inline int Get(int x) {
if (x == prt[x])
return x;
else
return prt[x] = Get(prt[x]);
}
inline void Addedge(int x, int y) {
a[++cnt].to = y;
a[cnt].next = h[x];
h[x] = cnt;
}
inline void Read() {
n = read();
m = read();
for (int i = 1; i <= m; i++) {
int x = read(), y = read();
Addedge(x, y);
Addedge(y, x);
}
for (int i = 1; i <= n; i++) prt[i] = i;
}
int Deep[maxn], Act[maxn], Aux[maxn], fa[maxn];
inline void Tarjan(int x, int dep) {
Deep[x] = dep;
for (int i = h[x]; i; i = a[i].next) {
int y = a[i].to;
if (!Deep[y]) {
fa[y] = x;
Tarjan(y, dep + 1);
if (Get(x) == Get(y)) Aux[x] |= Aux[y];
} else if (Deep[x] > Deep[y] + 1) {
if ((Deep[x] - Deep[y]) % 2 == 0) Aux[x] = 1;
for (int k = Get(x); Deep[k] > Deep[y] + 1; k = Get(k)) prt[k] = fa[k];
}
}
}
inline void DFS(int x) {
Act[x] = Act[fa[x]] + Aux[x];
for (int i = h[x]; i; i = a[i].next) {
int y = a[i].to;
if (fa[y] == x) {
if (Get(x) == Get(y)) Aux[y] |= Aux[x];
DFS(y);
}
}
}
int p[maxn][20];
inline void ST() {
memset(p, -1, sizeof(p));
for (int i = 1; i <= n; i++) p[i][0] = fa[i];
for (int j = 1; j <= (int)(log(n) / log(2)); j++)
for (int i = 1; i <= n; i++)
if (p[i][j - 1] != -1) p[i][j] = p[p[i][j - 1]][j - 1];
}
inline int LCA(int a, int b) {
if (Deep[a] < Deep[b]) swap(a, b);
int k = (int)(log(Deep[a]) / log(2));
for (int i = k; i >= 0; i--)
if (Deep[a] - (1 << i) >= Deep[b]) a = p[a][i];
if (a == b) return a;
for (int i = k; i >= 0; i--)
if (p[a][i] != -1 && p[a][i] != p[b][i]) a = p[a][i], b = p[b][i];
return p[a][0];
}
inline bool Check(int x, int y) {
int fa = LCA(x, y);
if (!fa) return false;
if ((Deep[x] + Deep[y]) % 2) return true;
if (Act[x] + Act[y] - 2 * Act[fa] > 0) return true;
return false;
}
int main() {
Read();
for (int i = 1; i <= n; i++)
if (!Deep[i]) Tarjan(i, 1);
for (int i = 1; i <= n; i++)
if (Deep[i] == 1) DFS(i);
ST();
q = read();
for (int i = 1; i <= q; i++) {
int x = read(), y = read();
if (Check(x, y))
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, dfk[100010];
int dfn[100010], low[100010];
int ver[200010], nex[200010], from[200010], head[100010], nu, num;
int x1, x2, x3, x4, root;
int sd[100010];
int fa[100010];
int Stack[100010], top;
int qqq;
int cnt, ccc[100010];
int q[100010];
vector<int> dcc[100010];
int fp[100010][18];
inline void add(int x, int y) {
ver[++nu] = y, nex[nu] = head[x], from[nu] = x, head[x] = nu;
return;
}
inline int read() {
int x = 0;
char c = getchar();
bool y = 1;
for (; c < '0' || c > '9'; c = getchar())
if (c == '-') y = 0;
for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + c - 48;
if (y) return x;
return -x;
}
void dfs(int x, int fx) {
fa[x] = root;
sd[x] = sd[fx] + 1;
fp[x][0] = fx;
for (int ik = 0; ik < 17; ik++) fp[x][ik + 1] = fp[fp[x][ik]][ik];
dfn[x] = low[x] = ++num;
Stack[++top] = x;
if (x == root && head[x] == 0) {
dcc[++cnt].push_back(x);
Stack[top--] = 0;
return;
}
int tj = 0;
for (int i = head[x]; i; i = nex[i]) {
int y = ver[i];
if (!dfn[y]) {
dfs(y, x);
low[x] = min(low[x], low[y]);
if (low[y] >= dfn[x]) {
cnt++;
int z = 0;
do {
z = Stack[top--];
dcc[cnt].push_back(z);
ccc[z] = cnt;
} while (z != y);
}
} else
low[x] = min(low[x], dfn[y]);
}
}
inline int LCA(int x, int y) {
if (sd[x] < sd[y]) {
int t = x;
x = y;
y = t;
}
for (int i = 17; i >= 0; i--)
if (sd[fp[x][i]] >= sd[y]) {
x = fp[x][i];
if (x == y) return x;
}
for (int i = 17; i >= 0; i--)
if (fp[x][i] != fp[y][i]) x = fp[x][i], y = fp[y][i];
return fp[x][0];
}
void Dfs(int x) {
dfk[x] = 1;
for (int i = head[x]; i; i = nex[i]) {
int y = ver[i];
if (dfk[y]) continue;
q[y] += q[x];
Dfs(y);
}
return;
}
int main() {
n = read(), m = read();
for (int i = 1; i <= m; i++) {
x1 = read(), x2 = read();
add(x1, x2);
add(x2, x1);
}
for (int i = 1; i <= n; i++)
if (!dfn[i]) root = i, dfs(i, i);
for (int i = 1; i <= m * 2; i++) {
int x = from[i], y = ver[i];
if (sd[x] > sd[y]) continue;
if (sd[x] % 2 == sd[y] % 2) {
if (!q[y]) {
for (int j = 0; j < dcc[ccc[y]].size(); j++) q[dcc[ccc[y]][j]] = 1;
}
}
}
for (int i = 1; i <= n; i++)
if (!dfk[i]) Dfs(i);
qqq = read();
for (int i = 1; i <= qqq; i++) {
x1 = read(), x2 = read();
if (x1 == x2) {
printf("No\n");
continue;
}
if (fa[x1] != fa[x2]) {
printf("No\n");
continue;
}
if ((sd[x1] & 1) != (sd[x2] & 1)) {
printf("Yes\n");
continue;
}
int lca = LCA(x1, x2);
if (q[x1] + q[x2] - 2 * q[lca] > 0) {
printf("Yes\n");
continue;
}
{
printf("No\n");
continue;
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
namespace FGF {
int n, m;
const int N = 5e5 + 5;
struct edg {
int to, nxt, id;
} e[N << 1];
int cnt = 1, head[N], s;
int rt[N], dfn[N], low[N], st[N], bel[N], col[N], num, tp, dcc, siz[N], sum[N],
dep[N], fa[N][20];
bool vis[N], is[N], fl;
vector<int> V[N], E[N];
void add(int u, int v, int id) {
cnt++;
e[cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt;
e[cnt].id = id;
}
void tarjan(int u, int f) {
dfn[u] = low[u] = ++num;
dep[u] = dep[e[f ^ 1].to] + 1, fa[u][0] = e[f ^ 1].to,
rt[u] = rt[e[f ^ 1].to];
for (int i = 1; i <= 18; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (vis[i] || i == (f ^ 1)) continue;
st[++tp] = i;
if (!dfn[v]) {
tarjan(v, i);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
++dcc;
int x = -1;
V[dcc].push_back(u);
do {
x = st[tp--];
bel[x] = bel[x ^ 1] = dcc;
vis[x] = vis[x ^ 1] = 1;
V[dcc].push_back(e[x].to);
E[dcc].push_back(x);
} while (x != i);
}
} else
low[u] = min(low[u], dfn[v]);
}
}
bool dfs2(const int u, const int from) {
if (vis[u]) {
if (col[u] == col[e[from ^ 1].to])
return true;
else
return false;
}
vis[u] = true, col[u] = (col[e[from ^ 1].to] ^ 1);
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (bel[i] != bel[from] || i == (from ^ 1)) continue;
if (dfs2(v, i)) return true;
}
return false;
}
void dfs(const int u, const int from) {
vis[u] = true;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (vis[v] || i == (from ^ 1)) continue;
sum[v] = sum[u] + is[i];
dfs(v, i);
}
}
int getlca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
int d = dep[u] - dep[v];
for (int i = 18; i >= 0; i--)
if (d & (1 << i)) u = fa[u][i];
if (u == v) return u;
for (int i = 18; i >= 0; i--)
if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
return fa[u][0];
}
void work() {
scanf("%d%d", &n, &m);
int u, v;
for (int i = 1; i <= m; i++) {
scanf("%d%d", &u, &v);
add(u, v, i), add(v, u, i);
}
for (int i = 1; i <= n; i++)
if (!dfn[i]) rt[0] = i, tarjan(i, 0);
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= dcc; i++) {
for (int sz = V[i].size(), j = 0; j < sz; j++) vis[V[i][j]] = 0;
if (dfs2(e[E[i][0]].to, E[i][0]))
for (int sz = E[i].size(), j = 0; j < sz; j++)
is[E[i][j]] = is[E[i][j] ^ 1] = 1;
}
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++)
if (!vis[i]) dfs(i, 0);
int Q;
scanf("%d", &Q);
while (Q--) {
scanf("%d%d", &u, &v);
if (rt[u] != rt[v])
puts("No");
else {
int lca = getlca(u, v);
if ((dep[u] + dep[v] - 2 * dep[lca]) & 1)
puts("Yes");
else if (sum[u] + sum[v] - 2 * sum[lca])
puts("Yes");
else
puts("No");
}
}
}
} // namespace FGF
int main() {
FGF::work();
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
const int N = 1e5 + 2;
int n, m, q, cnf, col[N], bel[N << 1];
bool eve[N << 1];
template <class T>
inline void apn(T &x, const T y) {
if (x > y) x = y;
}
inline char get_c() {
static char *h, *t, buf[200000];
if (h == t) {
t = (h = buf) + fread(buf, 1, 200000, stdin);
if (h == t) return EOF;
}
return *h++;
}
inline int nxi() {
int x = 0;
char c;
while ((c = get_c()) > '9' || c < '0')
;
while (x = x * 10 - 48 + c, (c = get_c()) >= '0' && c <= '9')
;
return x;
}
namespace G {
int cnt = 1, fir[N], dfn[N], low[N];
bool vis[N << 1];
struct edge {
int fr, to, nx;
} eg[N << 1];
inline void add_edge(const int a, const int b) {
eg[++cnt] = (edge){a, b, fir[a]};
fir[a] = cnt;
}
void tarjan(const int x) {
static int cnd, top, stk[N << 1];
low[x] = dfn[x] = ++cnd;
for (int i = fir[x]; i; i = eg[i].nx) {
const int y = eg[i].to;
if (!vis[i]) stk[++top] = i;
if (dfn[y]) {
apn(low[x], dfn[y]);
} else {
tarjan(y);
if (low[y] >= dfn[x]) {
int j = 0;
++cnf;
while (j != i) {
j = stk[top--];
bel[j] = bel[j ^ 1] = cnf;
vis[j] = vis[j ^ 1] = 1;
}
} else
apn(low[x], low[y]);
}
}
}
void paint(const int x) {
for (int i = fir[x]; i; i = eg[i].nx) {
const int y = eg[i].to;
if (~col[y]) {
if (col[y] == col[x]) eve[bel[i]] = 1;
} else {
col[y] = col[x] ^ 1;
paint(y);
}
}
}
} // namespace G
namespace T {
int cnt, dep[N], rt[N], fir[N], top[N], sz[N], son[N], fa[N], exi[N];
struct edge {
int to, nx;
} eg[N];
inline void add(const int a, const int b) {
eg[++cnt] = (edge){b, fir[a]};
fir[a] = cnt;
}
void dfs1(const int x) {
sz[x] = 1;
for (int i = G::fir[x]; i; i = G::eg[i].nx) {
const int y = G::eg[i].to;
if (!sz[y]) {
add(x, y);
rt[y] = rt[x];
fa[y] = x;
dep[y] = dep[x] + 1;
dfs1(y);
if (sz[y] > sz[son[x]]) son[x] = y;
sz[x] += sz[y];
}
}
}
void dfs2(const int x) {
exi[x] += exi[fa[x]];
top[x] = son[fa[x]] == x ? top[fa[x]] : x;
if (son[x]) dfs2(son[x]);
for (int i = fir[x]; i; i = eg[i].nx) {
const int y = eg[i].to;
if (!top[y]) dfs2(y);
}
}
inline int lca(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] > dep[top[y]])
x = fa[top[x]];
else
y = fa[top[y]];
}
return dep[x] < dep[y] ? x : y;
}
} // namespace T
int main() {
memset(col, -1, sizeof(col));
n = nxi(), m = nxi();
for (int i = 1; i <= m; ++i) {
const int a = nxi(), b = nxi();
G::add_edge(a, b);
G::add_edge(b, a);
}
for (int i = 1; i <= n; ++i) {
if (!G::dfn[i]) G::tarjan(i);
}
for (int i = 1; i <= n; ++i) {
if (col[i] == -1) col[i] = 0, G::paint(i);
}
for (int i = 1; i <= n; ++i) {
if (!T::dep[i]) T::rt[i] = i, T::dfs1(i);
}
for (int i = 2; i <= G::cnt; ++i) {
using G::eg;
using T::fa;
if (eve[bel[i]]) {
const int x = eg[i].fr, y = eg[i].to;
if (fa[x] != y && fa[y] != x) continue;
T::exi[fa[x] == y ? x : y] = 1;
}
}
for (int i = 1; i <= n; ++i) {
if (!T::top[i]) T::dfs2(i);
}
q = nxi();
while (q--) {
const int x = nxi(), y = nxi();
if (T::rt[x] != T::rt[y]) {
puts("No");
continue;
}
const int z = T::lca(x, y);
if ((T::dep[x] + T::dep[y] - (T::dep[z] << 1)) & 1)
puts("Yes");
else
puts(T::exi[x] + T::exi[y] > T::exi[z] << 1 ? "Yes" : "No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline int inn() {
int x, ch;
while ((ch = getchar()) < '0' || ch > '9')
;
x = ch ^ '0';
while ((ch = getchar()) >= '0' && ch <= '9')
x = (x << 1) + (x << 3) + (ch ^ '0');
return x;
}
int d[100010], up[100010][20], Log[100010], vis[100010], bel[100010],
id[100010], ql[100010];
int qx[100010], qy[100010], isno[100010];
long long val[100010];
vector<int> g[100010], to[100010];
long long dfs(int x, int fa, int cnt) {
memset(up[x], 0, sizeof up[x]);
d[x] = d[fa] + 1, vis[x] = 1, bel[x] = cnt, up[x][0] = fa;
for (int i = 1; i <= Log[d[x]]; i++) up[x][i] = up[up[x][i - 1]][i - 1];
for (int i = 0, y; i < (int)to[x].size(); i++)
if ((y = to[x][i]) ^ fa) {
if (!vis[y])
val[x] += dfs(y, x, cnt), g[x].push_back(y);
else if (vis[y] == 1 && (d[x] & 1) == (d[y] & 1))
val[x]++, val[y]--;
}
return vis[x] = 2, val[x];
}
int getval(int x, int fa = 0) {
val[x] += val[fa];
for (int i = 0; i <= (int)g[x].size() - 1; i++) getval(g[x][i], x);
return 0;
}
inline int getLCA(int x, int y) {
if (d[x] < d[y]) swap(x, y);
for (int i = Log[d[x]]; i >= 0; i--)
if (d[up[x][i]] >= d[y]) x = up[x][i];
if (x == y) return x;
for (int i = Log[d[x]]; i >= 0; i--)
if (up[x][i] ^ up[y][i]) x = up[x][i], y = up[y][i];
return up[x][0];
}
char ss[1000000];
int ssl;
int main() {
srand((unsigned int)(time(0) - 20010817 - 20010916));
int n = inn(), m = inn(), x, y;
for (int i = 2; i <= n; i++) Log[i] = Log[i >> 1] + 1;
for (int i = 1; i <= m; i++)
x = inn(), y = inn(), to[x].push_back(y), to[y].push_back(x);
int q = inn(), qcnt = q;
for (int i = 1; i <= n; i++) id[i] = i;
for (int i = 1; i <= q; i++) qx[i] = inn(), qy[i] = inn(), ql[i] = i;
for (int Tms = 25; qcnt && Tms; Tms--) {
for (int x = 1; x <= n; x++) g[x].clear(), val[x] = 0;
int cnt = 0;
for (int x = 1; x <= n; x++) random_shuffle(to[x].begin(), to[x].end());
memset(vis, 0, sizeof(int) * (n + 1)), random_shuffle(id + 1, id + n + 1);
for (int i = 1; i <= n; i++)
if (!vis[id[i]]) d[id[i]] = 0, dfs(id[i], 0, ++cnt), getval(id[i]);
for (int i = 1; i <= qcnt; i++) {
int x = qx[ql[i]], y = qy[ql[i]];
if (bel[x] ^ bel[y]) {
isno[ql[i]] = 1;
continue;
}
if ((d[x] & 1) != (d[y] & 1)) {
isno[ql[i]] = 0;
continue;
}
int c = getLCA(x, y);
if (val[x] + val[y] - 2 * val[c])
isno[ql[i]] = 0;
else
isno[ql[i]] = 1;
}
int qc = 0;
for (int i = 1; i <= qcnt; i++)
if (isno[ql[i]]) ql[++qc] = ql[i];
qcnt = qc;
}
for (int i = 1; i <= q; i++)
if (isno[i])
ss[++ssl] = 'N', ss[++ssl] = 'o', ss[++ssl] = '\n';
else
ss[++ssl] = 'Y', ss[++ssl] = 'e', ss[++ssl] = 's', ss[++ssl] = '\n';
return fwrite(ss + 1, sizeof(char), ssl, stdout), 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
namespace acah {
template <typename T>
inline void qread(T &x) {
x = 0;
char ch = getchar();
while (!isdigit(ch)) ch = getchar();
while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
}
const int maxn = 1e5 + 7;
int N, M, Q;
int ac[maxn << 1];
struct edge {
int to, nt;
} e[maxn << 1];
int hd[maxn], _ = 1;
inline void ins(int u, int v) { e[++_].to = v, e[_].nt = hd[u], hd[u] = _; }
int ind, dfn[maxn], low[maxn], bel[maxn << 1];
int st[maxn << 1], tp, ndcc;
int fa[maxn][20], rt[maxn], dep[maxn];
bool vis[maxn << 1];
vector<int> C[maxn], E[maxn << 1];
void Tarjan(int u, int f) {
dfn[u] = low[u] = ++ind;
fa[u][0] = e[f ^ 1].to, rt[u] = rt[fa[u][0]], dep[u] = dep[fa[u][0]] + 1;
for (int i = 1; i <= 18; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
for (int i = hd[u]; i; i = e[i].nt) {
if (vis[i] || i == (f ^ 1)) continue;
st[++tp] = i;
int v = e[i].to;
if (!dfn[v]) {
Tarjan(v, i);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
int cur;
C[++ndcc].push_back(u);
do {
cur = st[tp--];
bel[cur] = bel[cur ^ 1] = ndcc;
vis[cur] = vis[cur ^ 1] = true;
C[ndcc].push_back(e[cur].to);
E[ndcc].push_back(cur);
} while (cur != i);
}
} else
low[u] = min(low[u], dfn[v]);
}
}
int col[maxn];
bool color(int u, int f) {
int fath = e[f ^ 1].to;
if (vis[u]) return (col[u] == col[fath]);
vis[u] = true, col[u] = col[fath] ^ 1;
for (int i = hd[u]; i; i = e[i].nt) {
if (bel[i] != bel[f] || i == (f ^ 1)) continue;
int v = e[i].to;
if (color(v, i)) return true;
}
return false;
}
int cnt[maxn];
void count(int u, int f) {
vis[u] = true;
for (int i = hd[u]; i; i = e[i].nt) {
int v = e[i].to;
if (vis[v] || i == (f ^ 1)) continue;
cnt[v] = cnt[u] + ac[i];
count(v, i);
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
int dif = dep[u] - dep[v];
for (int i = 18; i >= 0; i--)
if (dif & (1 << i)) u = fa[u][i];
if (u == v) return u;
for (int i = 18; i >= 0; i--)
if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
return fa[u][0];
}
int work() {
qread(N), qread(M);
for (int i = 1, u, v; i <= M; i++) qread(u), qread(v), ins(u, v), ins(v, u);
for (int i = 1; i <= N; i++)
if (!dfn[i]) rt[0] = i, Tarjan(i, 0);
memset(vis, 0, sizeof vis);
for (int i = 1; i <= ndcc; i++) {
for (int s = C[i].size(), j = 0; j < s; j++) vis[C[i][j]] = false;
if (color(e[E[i][0]].to, E[i][0]))
for (int s = E[i].size(), j = 0; j < s; j++)
ac[E[i][j]] = ac[E[i][j] ^ 1] = 1;
}
memset(vis, 0, sizeof vis);
for (int i = 1; i <= N; i++)
if (!vis[i]) count(i, 0);
qread(Q);
for (int q = 1, u, v; q <= Q; q++) {
qread(u), qread(v);
if (rt[u] != rt[v])
puts("No");
else {
int l = lca(u, v);
if ((dep[u] + dep[v] - (dep[l] << 1)) & 1)
puts("Yes");
else if (cnt[u] + cnt[v] - (cnt[l] << 1))
puts("Yes");
else
puts("No");
}
}
return 0;
}
} // namespace acah
int main() { return acah::work(); }
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > e[100010];
int n, m, u, v, i, j, q, idx, root;
bool Intree[100010], vis[100010];
int depth[100010], S[100010], C[100010], F[100010], Pos[100010];
int st[100010 * 2][20][2];
void dfs(int a, int u) {
++idx;
st[idx][0][0] = depth[u];
st[idx][0][1] = u;
Pos[u] = idx;
F[u] = root;
vis[u] = 1;
for (typeof(e[u].begin()) it = e[u].begin(); it != e[u].end(); it++)
if (it->first != a) {
if (!vis[it->first]) {
Intree[it->second] = 1;
depth[it->first] = depth[u] + 1;
dfs(u, it->first);
++idx;
st[idx][0][0] = depth[u];
st[idx][0][1] = u;
} else {
if (depth[it->first] >= depth[u]) continue;
if ((depth[it->first] - depth[u]) % 2 == 0) {
S[it->first]--;
S[u]++;
}
}
}
}
void CountA_S(int a, int u) {
vis[u] = 1;
for (typeof(e[u].begin()) it = e[u].begin(); it != e[u].end(); it++)
if (it->first != a && Intree[it->second]) {
CountA_S(u, it->first);
S[u] += S[it->first];
}
}
void CountB_S(int a, int u) {
S[u] += S[a];
vis[u] = 1;
for (typeof(e[u].begin()) it = e[u].begin(); it != e[u].end(); it++)
if (it->first != a && Intree[it->second]) CountB_S(u, it->first);
}
void CountA_C(int a, int u) {
vis[u] = 1;
for (typeof(e[u].begin()) it = e[u].begin(); it != e[u].end(); it++)
if (it->first != a && Intree[it->second]) {
CountA_C(u, it->first);
C[u] += C[it->first];
}
}
void CountB_C(int a, int u) {
C[u] += C[a];
vis[u] = 1;
for (typeof(e[u].begin()) it = e[u].begin(); it != e[u].end(); it++)
if (it->first != a && Intree[it->second]) CountB_C(u, it->first);
}
int lg(int first) { return (int)floor(log(first) / log(2)); }
int LCA(int u, int v) {
u = Pos[u];
v = Pos[v];
if (u > v) swap(u, v);
int l = lg(v - u + 1);
if (st[u][l][0] < st[v - (1 << l) + 1][l][0])
return st[u][l][1];
else
return st[v - (1 << l) + 1][l][1];
}
int main() {
scanf("%d %d\n", &n, &m);
for (i = 1; i <= m; i++) {
scanf("%d %d\n", &u, &v);
e[u].push_back(make_pair(v, i));
e[v].push_back(make_pair(u, i));
}
memset(vis, 0, sizeof(vis));
for (i = 1; i <= n; i++)
if (!vis[i]) {
root = i;
dfs(0, i);
}
for (j = 1; j <= lg(idx); j++)
for (i = 1; i <= idx - (1 << j) + 1; i++)
if (st[i][j - 1][0] < st[i + (1 << (j - 1))][j - 1][0]) {
st[i][j][0] = st[i][j - 1][0];
st[i][j][1] = st[i][j - 1][1];
} else {
st[i][j][0] = st[i + (1 << (j - 1))][j - 1][0];
st[i][j][1] = st[i + (1 << (j - 1))][j - 1][1];
}
memset(vis, 0, sizeof(vis));
for (i = 1; i <= n; i++)
if (!vis[i]) CountA_S(0, i);
memset(vis, 0, sizeof(vis));
for (i = 1; i <= n; i++)
if (!vis[i]) CountB_S(0, i);
for (u = 1; u <= n; u++)
for (typeof(e[u].begin()) it = e[u].begin(); it != e[u].end(); it++)
if (!Intree[it->second]) {
if (depth[it->first] >= depth[u]) continue;
if ((depth[u] - depth[it->first]) & 1) {
if (S[u] - S[it->first] > 0) {
C[u]++;
C[it->first]--;
}
}
}
memset(vis, 0, sizeof(vis));
for (i = 1; i <= n; i++)
if (!vis[i]) CountA_C(0, i);
memset(vis, 0, sizeof(vis));
for (i = 1; i <= n; i++)
if (!vis[i]) CountB_C(0, i);
scanf("%d\n", &q);
for (i = 1; i <= q; i++) {
scanf("%d %d\n", &u, &v);
if (F[u] != F[v]) {
printf("No\n");
continue;
}
int z = LCA(u, v);
j = depth[u] + depth[v] - depth[z] * 2;
if (j & 1) {
printf("Yes\n");
continue;
}
if (S[u] + S[v] - S[z] * 2 > 0) {
printf("Yes\n");
continue;
}
if (C[u] + C[v] - C[z] * 2 > 0)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int p[400020], n1[400020], h[200010], ee = 0, q[200010], up[200010],
anc[20][200010], n, m, Q, d[200010],
sum[200010], tot = 0;
void ae(int x, int y) {
p[ee] = y;
n1[ee] = h[x];
h[x] = ee++;
}
int lca(int x, int y) {
if (d[x] < d[y]) swap(x, y);
int k = d[x] - d[y], j = 0;
for (int k = d[x] - d[y], j = 0; k; k >>= 1, j++)
if (k & 1) x = anc[j][x];
if (x == y) return x;
for (int i = 17; ~i; i--)
if (anc[i][x] != anc[i][y]) x = anc[i][x], y = anc[i][y];
return anc[0][x];
}
void dfs(int u) {
q[++tot] = u;
for (int i = h[u]; ~i; i = n1[i]) {
if (!~d[p[i]]) {
d[p[i]] = d[u] + 1;
anc[0][p[i]] = u;
dfs(p[i]);
} else if (d[u] - d[p[i]] > 1 && (d[u] - d[p[i]]) % 2 == 0)
up[u] = max(up[u], d[p[i]]);
}
}
pair<int, int> qq[200010];
vector<int> g[200010];
void check() {
int tail;
for (int i = 1; i <= n; i++)
if (d[i] == -1) {
d[i] = 0;
qq[1] = make_pair(i, 0);
tail = 1;
q[++tot] = i;
while (tail) {
int &x = qq[tail].first, &e = qq[tail].second;
if (e >= g[x].size()) {
--tail;
continue;
}
for (; e < g[x].size(); e++) {
int y = g[x][e];
if (d[y] == -1) {
q[++tot] = y;
d[y] = d[x] + 1;
anc[0][y] = x;
qq[++tail] = make_pair(y, 0);
break;
} else {
if (d[x] - d[y] > 1 && (d[x] - d[y]) % 2 == 0) {
up[x] = max(up[x], d[y]);
}
}
}
}
}
}
void init() {
int s = 1, e = 1;
memset(d, -1, sizeof(d));
memset(up, -1, sizeof(up));
check();
for (int i = 1; i <= n; i++)
if (anc[0][q[i]]) up[q[i]] = max(up[q[i]], up[anc[0][q[i]]]);
for (int i = 1; i <= n; i++) {
for (int j = h[i]; ~j; j = n1[j]) {
int v = p[j];
if (d[i] >= d[v] || anc[0][v] == i || anc[0][i] == v) continue;
if ((d[v] - d[i]) & 1) {
if (up[v] >= d[i]) --sum[i], ++sum[v];
} else
--sum[i], ++sum[v];
}
}
for (int i = n; i; i--)
if (anc[0][q[i]]) sum[anc[0][q[i]]] += sum[q[i]];
for (int i = 1; i <= n; i++)
if (anc[0][q[i]]) sum[q[i]] += sum[anc[0][q[i]]];
for (int i = 1; i <= 17; i++)
for (int j = 1; j <= n; j++) anc[i][j] = anc[i - 1][anc[i - 1][j]];
}
int main() {
scanf("%d%d", &n, &m);
memset(h, -1, sizeof(h));
int x, y;
for (int i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
ae(x, y);
ae(y, x);
}
init();
scanf("%d", &Q);
while (Q--) {
scanf("%d%d", &x, &y);
int l = lca(x, y), fl = 0;
if (l >= 1 && l <= n && x != y)
if ((d[x] + d[y]) % 2 == 1 || sum[x] + sum[y] - 2 * sum[l] > 0) fl = 1;
if (fl)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int n, m, q, curcol, col[100010], depth[100010], val[100010], sum[100010],
par[20][100010], parr[100010];
bool vis[100010];
vector<int> g[100010];
int FIND(int pos) {
if (parr[pos] != pos) parr[pos] = FIND(parr[pos]);
return parr[pos];
}
void dfs(int pos, int no, int d) {
vis[pos] = true;
col[pos] = curcol;
par[0][pos] = no;
depth[pos] = d;
for (int i = 0; i < g[pos].size(); i++) {
if (g[pos][i] == no) continue;
if (vis[g[pos][i]]) {
if (depth[pos] > depth[g[pos][i]] + 1) {
if ((depth[pos] - depth[g[pos][i]]) % 2 == 0) val[pos] = 1;
for (int j = FIND(pos); depth[j] > depth[g[pos][i]] + 1; j = FIND(j))
parr[j] = par[0][j];
}
continue;
}
dfs(g[pos][i], pos, d + 1);
if (FIND(pos) == FIND(g[pos][i])) val[pos] |= val[g[pos][i]];
}
}
void dfs2(int pos) {
sum[pos] += val[pos];
for (int i = 0; i < g[pos].size(); i++) {
if (depth[g[pos][i]] != depth[pos] + 1) continue;
if (FIND(pos) == FIND(g[pos][i])) val[g[pos][i]] |= val[pos];
sum[g[pos][i]] = sum[pos];
dfs2(g[pos][i]);
}
}
int lca(int pos, int pos2) {
if (depth[pos] < depth[pos2]) swap(pos, pos2);
for (int i = 18; i >= 0; i--)
if (depth[pos] - (int)pow(2, i) >= depth[pos2]) pos = par[i][pos];
for (int i = 18; i >= 0; i--) {
if (par[i][pos] != par[i][pos2]) {
pos = par[i][pos];
pos2 = par[i][pos2];
}
}
if (pos == pos2) return pos;
return par[0][pos];
}
int main() {
cin >> n >> m;
int x, y;
for (int i = 0; i < m; i++) {
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 1; i <= n; i++) parr[i] = i;
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
curcol = i;
dfs(i, 0, 0);
dfs2(i);
}
for (int i = 0; i < 18; i++) {
for (int j = 1; j <= n; j++) {
if (par[i][j] == 0) continue;
par[i + 1][j] = par[i][par[i][j]];
}
}
cin >> q;
for (int qn = 0; qn < q; qn++) {
scanf("%d%d", &x, &y);
int z = lca(x, y);
if (z == 0)
puts("No");
else {
if ((depth[x] - depth[z] + depth[y] - depth[z]) % 2 == 1 ||
sum[x] + sum[y] - sum[z] * 2 > 0)
puts("Yes");
else
puts("No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int h[MAXN], n, m, q, par[MAXN], t1, t2;
bool seenv[MAXN], seen[MAXN], D[MAXN], cann, col[MAXN];
int colcomp[MAXN], recomp[MAXN];
pair<int, int> edg[MAXN];
vector<int> mat[MAXN];
set<int> S;
int gettar(int p, int v) {
if (edg[p].first == v) return edg[p].second;
return edg[p].first;
}
int dfs(int v, int hi) {
int maxih = hi, tmp;
h[v] = hi;
seenv[v] = true;
for (int i = 0; i < mat[v].size(); i++) {
if (seen[mat[v][i]]) continue;
int t = gettar(mat[v][i], v);
if (!seenv[t]) {
seen[mat[v][i]] = true;
tmp = dfs(t, hi + 1);
maxih = min(tmp, maxih);
if (tmp <= hi) par[t] = v;
} else
maxih = min(maxih, h[t]);
}
return maxih;
}
int getpar(int v) {
if (v == par[v]) return v;
par[v] = getpar(par[v]);
return par[v];
}
void dfsbip(int v, bool cole, int pari) {
seenv[v] = true;
col[v] = cole;
for (int i = 0; i < mat[v].size(); i++) {
int t = gettar(mat[v][i], v);
if (getpar(t) == pari)
if (!seenv[t])
dfsbip(t, !cole, pari);
else if (!cole != col[t])
cann = false;
}
}
void dfscol(int v, bool cole, int compid) {
if (D[getpar(v)] && S.find(getpar(v)) != S.end()) return;
if (D[getpar(v)]) S.insert(getpar(v));
seenv[v] = true;
col[v] = cole;
colcomp[v] = compid;
for (int i = 0; i < mat[v].size(); i++) {
int t = gettar(mat[v][i], v);
if (!seenv[t]) dfscol(t, !cole, compid);
}
}
void dfscon(int v, int compid) {
seenv[v] = true;
recomp[v] = compid;
for (int i = 0; i < mat[v].size(); i++) {
int t = gettar(mat[v][i], v);
if (!seenv[t]) dfscon(t, compid);
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) par[i] = i;
for (int i = 0; i < m; i++) {
cin >> t1 >> t2;
edg[i] = {t1, t2};
mat[t1].push_back(i);
mat[t2].push_back(i);
}
for (int i = 1; i <= n; i++)
if (!seenv[i]) dfs(i, 0);
fill_n(seenv, n + 1, 0);
for (int i = 1; i <= n; i++)
if (par[i] == i) {
cann = true;
dfsbip(i, 1, i);
if (!cann) D[i] = 1;
}
fill_n(seenv, n + 1, 0);
fill_n(col, n + 1, 0);
int c = 1;
for (int i = 1; i <= n; i++)
if (!seenv[i]) {
S.clear();
dfscol(i, 0, c);
c++;
}
c = 1;
fill_n(seenv, n + 1, 0);
for (int i = 1; i <= n; i++)
if (!seenv[i]) dfscon(i, c), c++;
cin >> q;
for (int i = 0; i < q; i++) {
cin >> t1 >> t2;
if (recomp[t1] != recomp[t2])
cout << "No" << endl;
else if (colcomp[t1] != colcomp[t2])
cout << "Yes" << endl;
else if (col[t1] == col[t2])
cout << "No" << endl;
else
cout << "Yes" << endl;
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
const int maxm = 17;
struct Edge {
int u, v, id;
} ss[maxn];
vector<Edge> E;
int n, m, q, etop;
vector<pair<int, int> > G[maxn];
int dep[maxn], pa[maxn][maxm], val[maxn];
int dfn[maxn], low[maxn], idn;
int col[maxn], col_cnt;
int fa[maxn], w[maxn];
bool odd[maxn], vis[maxn];
int root(int x) {
if (fa[x] == x) return x;
int rt = root(fa[x]);
w[x] ^= w[fa[x]];
return fa[x] = rt;
}
bool unite(int u, int v) {
int x = root(u), y = root(v);
if (x == y)
return !(w[u] ^ w[v] ^ 1);
else {
w[y] = w[u] ^ w[v] ^ 1;
fa[y] = x;
return 1;
}
}
void tarjan(int u, int eid) {
dfn[u] = low[u] = ++idn;
col[u] = col_cnt;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i].second, id = G[u][i].first;
if (id == eid) continue;
if (!dfn[v]) {
ss[++etop] = Edge{u, v, id};
tarjan(v, id);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
E.clear();
while (1) {
E.push_back(ss[etop--]);
if (E.back().id == id) break;
}
for (auto& e : E) {
fa[e.u] = e.u;
fa[e.v] = e.v;
w[e.u] = w[e.v] = 0;
}
bool flg = 1;
for (auto& e : E)
if (!unite(e.u, e.v)) {
flg = 0;
break;
}
if (!flg)
for (auto& e : E) odd[e.id] = 1;
}
} else if (dfn[v] < dfn[u]) {
ss[++etop] = Edge{u, v, id};
low[u] = min(low[u], dfn[v]);
}
}
}
void dfs(int u, int fa) {
vis[u] = 1;
dep[u] = dep[fa] + 1;
pa[u][0] = fa;
for (int i = 1; i < maxm; i++) pa[u][i] = pa[pa[u][i - 1]][i - 1];
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i].second, id = G[u][i].first;
if (vis[v]) continue;
val[v] = val[u] + odd[id];
dfs(v, u);
}
}
int getlca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
int d = dep[u] - dep[v];
for (int i = maxm - 1; i >= 0; i--)
if (d >> i & 1) u = pa[u][i];
if (u == v) return u;
for (int i = maxm - 1; i >= 0; i--) {
if (pa[u][i] != pa[v][i]) {
u = pa[u][i];
v = pa[v][i];
}
}
return pa[u][0];
}
inline int getd(int u, int v) {
int lca = getlca(u, v);
return dep[u] + dep[v] - 2 * dep[lca];
}
inline int getx(int u, int v) {
int lca = getlca(u, v);
return val[u] + val[v] - 2 * val[lca];
}
int main() {
cin >> n >> m;
for (int i = 1, u, v; i <= m; i++) {
cin >> u >> v;
G[u].push_back(make_pair(i, v));
G[v].push_back(make_pair(i, u));
}
for (int i = 1; i <= n; i++)
if (!dfn[i]) {
col_cnt++;
tarjan(i, 0);
}
for (int i = 1; i <= n; i++)
if (!vis[i]) dfs(i, 0);
cin >> q;
while (q--) {
int u, v;
cin >> u >> v;
if (col[u] != col[v] || !getx(u, v) && !(getd(u, v) & 1))
puts("No");
else
puts("Yes");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 5e5;
const int LOGN = 20;
int tempo, nz;
int vis[N], low[N], seen[N];
int gcol[N], col[N];
bool odd[N];
vector<int> adj[N], edges, g[N], t[N];
stack<int> st;
bool color(int u, int c) {
if (col[u] != -1) return col[u] == c;
col[u] = c;
for (int v : g[u])
if (!color(v, c ^ 1)) return false;
return true;
}
void tarjan(int u, int p) {
vis[u] = low[u] = ++tempo;
for (int k : adj[u]) {
int v = edges[k];
if (v == p) continue;
if (!seen[k]) {
seen[k] = seen[k ^ 1] = 1;
st.push(k);
}
if (!vis[v]) {
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= vis[u]) {
vector<int> e;
int cur;
do {
cur = st.top();
st.pop();
e.push_back(cur);
} while (cur != k);
vector<int> comp;
for (int kk : e)
comp.push_back(edges[kk]), comp.push_back(edges[kk ^ 1]);
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
for (int w : comp) g[w].clear(), col[w] = -1;
for (int kk : e)
g[edges[kk ^ 1]].push_back(edges[kk]),
g[edges[kk]].push_back(edges[kk ^ 1]);
odd[nz] = !color(comp.back(), 0);
for (int w : comp) {
t[w].push_back(nz);
t[nz].push_back(w);
}
nz++;
}
} else
low[u] = min(low[u], vis[v]);
}
}
void gcolor(int u, int c) {
if (vis[u]) return;
vis[u] = 1;
gcol[u] = c;
for (int k : adj[u]) gcolor(edges[k], c ^ 1);
}
int P[N][LOGN], odds[N][LOGN];
int sta[N], nd[N];
void dfs(int u, int p) {
sta[u] = ++tempo;
P[u][0] = p;
odds[u][0] = odd[u];
for (int i = 1; i < LOGN; i++)
if (P[u][i - 1] != -1)
P[u][i] = P[P[u][i - 1]][i - 1],
odds[u][i] = odds[P[u][i - 1]][i - 1] | odds[u][i - 1];
for (int v : t[u])
if (v != p) dfs(v, u);
nd[u] = tempo;
}
bool anc(int p, int x) { return sta[p] <= sta[x] && sta[x] <= nd[p]; }
int LCA(int u, int v) {
if (anc(u, v)) return u;
for (int i = LOGN - 1; i >= 0; i--) {
if (P[u][i] != -1 && !anc(P[u][i], v)) u = P[u][i];
}
if (P[u][0] != -1 && anc(P[u][0], v)) return P[u][0];
return -1;
}
int lift(int u, int p) {
if (u == p) return odd[u];
int res = 0;
for (int i = LOGN - 1; i >= 0; i--) {
if (P[u][i] != -1 && !anc(P[u][i], p)) {
res |= odds[u][i];
u = P[u][i];
}
}
return res | odds[u][1];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
nz = n;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--, b--;
adj[a].push_back(edges.size());
edges.push_back(b);
adj[b].push_back(edges.size());
edges.push_back(a);
}
for (int i = 0; i < n; i++)
if (!vis[i]) gcolor(i, 0);
memset(vis, 0, sizeof vis);
for (int i = 0; i < n; i++)
if (!vis[i]) tarjan(i, -1);
memset(P, -1, sizeof P);
for (int i = 0; i < nz; i++)
if (!sta[i]) dfs(i, -1);
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
a--, b--;
int w = LCA(a, b);
if (w == -1)
cout << "No" << endl;
else if (lift(a, w) || lift(b, w) || gcol[a] != gcol[b])
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int> > > Mat;
vector<int> Grafo;
vector<int> Entrada;
vector<int> Rev;
vector<int> Aristas;
stack<int> STACK;
vector<int> Backi;
int grafos;
bool dfs(int A, int i, int pos) {
Grafo[A] = grafos;
Entrada[A] = pos;
Backi[A] = pos;
bool J = false;
for (int I = 0; I < Mat[A].size(); I++) {
if (Mat[A][I].first != i) {
if (Grafo[Mat[A][I].first] == 0) {
STACK.push(Mat[A][I].second);
bool JJ = dfs(Mat[A][I].first, A, pos + 1);
Backi[A] = min(Backi[A], Backi[Mat[A][I].first]);
if (i == -1 || (i != -1 && Backi[Mat[A][I].first] >= Entrada[A])) {
if (JJ) {
int UU = STACK.top();
STACK.pop();
while (UU != Mat[A][I].second) {
Aristas[UU] = 1;
UU = STACK.top();
STACK.pop();
}
Aristas[Mat[A][I].second] = 1;
} else {
int UU = STACK.top();
STACK.pop();
while (UU != Mat[A][I].second) {
UU = STACK.top();
STACK.pop();
}
}
} else {
if (JJ) J = JJ;
}
} else if (Entrada[Mat[A][I].first] < Entrada[A]) {
if ((Entrada[A] - Entrada[Mat[A][I].first]) % 2 == 0) {
J = true;
}
STACK.push(Mat[A][I].second);
Backi[A] = min(Backi[A], Backi[Mat[A][I].first]);
}
}
}
return J;
}
int main() {
int N, A, B, C;
scanf("%d%d", &N, &A);
Mat.resize(N);
for (int I = 0; I < A; I++) {
scanf("%d%d", &B, &C);
B--;
C--;
Mat[B].push_back(make_pair(C, I));
Mat[C].push_back(make_pair(B, I));
}
Backi.resize(N);
Aristas.resize(A);
Grafo.resize(N);
grafos = 1;
Entrada.resize(N);
Entrada.resize(0);
Entrada.resize(N);
Rev.resize(N);
for (int I = 0; I < N; I++) {
if (Grafo[I] == 0) {
bool OO = dfs(I, -1, 0);
grafos++;
}
}
vector<int> Distancias(N);
vector<int> CC(N), Rev3(N);
int cont = 0;
int var;
for (int I = 0; I < N; I++) {
if (Rev3[I] == 0) {
cont++;
queue<int> COLA;
COLA.push(I);
Distancias[I] = 1;
CC[I] = cont;
Rev3[I] = 1;
while (!COLA.empty()) {
int KKK = COLA.front();
COLA.pop();
CC[KKK] = cont;
Rev3[KKK] = 1;
for (int I2 = 0; I2 < Mat[KKK].size(); I2++) {
var = Mat[KKK][I2].first;
if (Aristas[Mat[KKK][I2].second] == 0 && Rev3[var] == 0) {
Distancias[var] = Distancias[KKK] + 1;
COLA.push(var);
Rev3[var] = 1;
}
}
}
}
}
int MM, AA, BB;
scanf("%d", &MM);
for (int I = 0; I < MM; I++) {
scanf("%d%d", &AA, &BB);
AA--;
BB--;
if (Grafo[AA] != Grafo[BB] || AA == BB) {
printf("No\n");
} else {
if (CC[AA] != CC[BB])
printf("Yes\n");
else {
if ((Distancias[AA] + Distancias[BB] + 1) % 2 == 0) {
printf("Yes\n");
} else
printf("No\n");
}
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
const int maxn = 1e5 + 10, lg = 18;
int n, m, q, cid[maxn], csize = 0, H[maxn], low[maxn], ok[maxn], par[maxn][lg],
comp[maxn], c1;
bool dp[maxn][lg];
vector<int> G[maxn];
bool mark[maxn];
void dfs1(int a, int p) {
par[a][0] = p;
for (int i = 1; i < lg; i++) {
par[a][i] = par[par[a][i - 1]][i - 1];
}
low[a] = H[a];
mark[a] = 1;
for (int b : G[a]) {
if (!mark[b]) {
H[b] = H[a] + 1;
comp[b] = comp[a];
dfs1(b, a);
low[a] = min(low[a], low[b]);
} else if (b != p) {
low[a] = min(low[a], H[b]);
}
}
}
void dfs2(int a) {
mark[a] = 1;
for (int b : G[a]) {
if (!mark[b]) {
if (low[b] >= H[a]) {
cid[b] = csize++;
} else {
cid[b] = cid[a];
}
dfs2(b);
} else if (H[a] > H[b]) {
int dis = H[a] - H[b];
if ((dis & 1) == 0) {
ok[cid[a]] = 1;
}
}
}
}
void dfs3(int a) {
mark[a] = 1;
dp[a][0] = (ok[cid[a]]);
for (int b : G[a]) {
if (!mark[b]) {
dfs3(b);
}
}
}
bool lca(int a, int b) {
bool ans = 0;
if (H[a] > H[b]) swap(a, b);
for (int i = lg - 1; i >= 0; i--) {
if (H[par[b][i]] >= H[a]) {
ans |= dp[b][i];
b = par[b][i];
}
}
if (a == b) {
return ans;
}
for (int i = lg - 1; i >= 0; i--) {
if (par[b][i] != par[a][i]) {
ans |= dp[a][i];
ans |= dp[b][i];
a = par[a][i];
b = par[b][i];
}
}
ans |= dp[a][0];
ans |= dp[b][0];
return ans;
}
int32_t main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
a--, b--;
G[a].push_back(b);
G[b].push_back(a);
}
for (int i = 0; i < n; i++) {
if (!mark[i]) {
comp[i] = c1++;
dfs1(i, i);
}
}
fill(mark, mark + n, 0);
for (int i = 0; i < n; i++) {
if (!mark[i]) {
cid[i] = csize++;
dfs2(i);
}
}
fill(mark, mark + n, 0);
for (int i = 0; i < n; i++) {
if (!mark[i]) {
dfs3(i);
}
}
for (int j = 0; j < lg - 1; j++) {
for (int i = 0; i < n; i++) {
dp[i][j + 1] = dp[i][j] | dp[par[i][j]][j];
}
}
scanf("%d", &q);
while (q--) {
int a, b;
scanf("%d%d", &a, &b);
a--, b--;
if (H[a] > H[b]) swap(a, b);
if (a == b || comp[a] != comp[b]) {
printf("No\n");
continue;
}
if ((H[b] - H[a]) & 1) {
printf("Yes\n");
continue;
}
bool ans = lca(a, b);
printf(ans ? "Yes\n" : "No\n");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
const int N = 100005;
int n, m, cnt, last[N], ls[N], top, stack[N], tot, a[N], dep[N], fa[N][20],
val[N], tim, dfn[N], low[N], f[N];
bool vis[N], flag;
struct edge {
int to, next, w;
} e[N * 4];
int find(int x) {
if (f[x] == x)
return x;
else
return f[x] = find(f[x]);
}
void addedge(int u, int v) {
e[++cnt].to = v;
e[cnt].next = last[u];
last[u] = cnt;
e[++cnt].to = u;
e[cnt].next = last[v];
last[v] = cnt;
}
void add(int u, int v) {
e[++cnt].to = v;
e[cnt].next = ls[u];
ls[u] = cnt;
}
void work(int rt) {
flag = 0;
for (int i = 1; i <= tot; i++) {
int x = e[a[i]].to;
for (int j = ls[x]; j; j = e[j].next)
if (x != rt && dep[x] == dep[e[j].to]) flag = 1;
x = e[a[i] ^ 1].to;
for (int j = ls[x]; j; j = e[j].next)
if (x != rt && dep[x] == dep[e[j].to]) flag = 1;
}
if (flag) {
for (int i = 1; i <= tot; i++) e[a[i]].w = e[a[i] ^ 1].w = 1;
}
}
void tarjan(int x, int fa) {
dfn[x] = low[x] = ++tim;
dep[x] = dep[fa] ^ 1;
for (int i = last[x]; i; i = e[i].next) {
if (e[i].to == fa) continue;
if (!dfn[e[i].to]) {
stack[++top] = i;
tarjan(e[i].to, x);
low[x] = std::min(low[x], low[e[i].to]);
if (low[e[i].to] >= dfn[x]) {
int y = 0;
tot = 0;
while (y != i) {
y = stack[top];
top--;
a[++tot] = y;
}
work(x);
}
} else {
low[x] = std::min(low[x], dfn[e[i].to]);
if (dfn[e[i].to] < dfn[x]) add(x, e[i].to);
}
}
}
void pre(int x) {
vis[x] = 1;
dep[x] = dep[fa[x][0]] + 1;
for (int i = 1; i <= 16; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1];
for (int i = last[x]; i; i = e[i].next)
if (!vis[e[i].to]) {
fa[e[i].to][0] = x;
val[e[i].to] = val[x] + e[i].w;
pre(e[i].to);
}
}
int get_lca(int x, int y) {
if (dep[x] < dep[y]) std::swap(x, y);
for (int i = 16; i >= 0; i--)
if (dep[fa[x][i]] >= dep[y]) x = fa[x][i];
if (x == y) return x;
for (int i = 16; i >= 0; i--)
if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
int main() {
scanf("%d%d", &n, &m);
cnt = 1;
for (int i = 1; i <= n; i++) f[i] = i;
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
addedge(x, y);
if (find(x) != find(y)) f[find(x)] = find(y);
}
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i, 0);
for (int i = 1; i <= n; i++)
if (!vis[i]) pre(i);
int q;
scanf("%d", &q);
while (q--) {
int x, y;
scanf("%d%d", &x, &y);
int lca = get_lca(x, y);
if (find(x) != find(y))
puts("No");
else if ((dep[x] & 1) != (dep[y] & 1))
puts("Yes");
else if (val[x] + val[y] - val[lca] * 2)
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int> > > Mat;
vector<int> Grafo;
vector<int> Entrada;
vector<int> Rev;
vector<int> Aristas;
stack<int> STACK;
vector<int> Backi;
int grafos;
bool dfs(int A, int i, int pos) {
Grafo[A] = grafos;
Entrada[A] = pos;
Backi[A] = pos;
bool J = false;
for (int I = 0; I < Mat[A].size(); I++) {
if (Mat[A][I].first != i) {
if (Grafo[Mat[A][I].first] == 0) {
STACK.push(Mat[A][I].second);
bool JJ = dfs(Mat[A][I].first, A, pos + 1);
Backi[A] = min(Backi[A], Backi[Mat[A][I].first]);
if (i == -1 || (i != -1 && Backi[Mat[A][I].first] >= Entrada[A])) {
if (JJ) {
int UU = STACK.top();
STACK.pop();
while (UU != Mat[A][I].second) {
Aristas[UU] = 1;
UU = STACK.top();
STACK.pop();
}
Aristas[Mat[A][I].second] = 1;
} else {
int UU = STACK.top();
STACK.pop();
while (UU != Mat[A][I].second) {
UU = STACK.top();
STACK.pop();
}
}
} else {
if (JJ) J = JJ;
}
} else if (Entrada[Mat[A][I].first] < Entrada[A]) {
if ((Entrada[A] - Entrada[Mat[A][I].first]) % 2 == 0) {
J = true;
}
STACK.push(Mat[A][I].second);
Backi[A] = min(Backi[A], Backi[Mat[A][I].first]);
}
}
}
return J;
}
int main() {
int N, A, B, C;
scanf("%d%d", &N, &A);
Mat.resize(N);
for (int I = 0; I < A; I++) {
scanf("%d%d", &B, &C);
B--;
C--;
Mat[B].push_back(make_pair(C, I));
Mat[C].push_back(make_pair(B, I));
}
Backi.resize(N);
Aristas.resize(A);
Grafo.resize(N);
grafos = 1;
Entrada.resize(N);
Entrada.resize(0);
Entrada.resize(N);
Rev.resize(N);
for (int I = 0; I < N; I++) {
if (Grafo[I] == 0) {
bool OO = dfs(I, -1, 0);
grafos++;
}
}
vector<int> Distancias(N);
vector<int> CC(N), Rev3(N);
int cont = 0;
int var;
for (int I = 0; I < N; I++) {
if (Rev3[I] == 0) {
cont++;
queue<int> COLA;
COLA.push(I);
Distancias[I] = 1;
CC[I] = cont;
Rev3[I] = 1;
while (!COLA.empty()) {
int KKK = COLA.front();
COLA.pop();
CC[KKK] = cont;
Rev3[KKK] = 1;
for (int I2 = 0; I2 < Mat[KKK].size(); I2++) {
var = Mat[KKK][I2].first;
if (Aristas[Mat[KKK][I2].second] == 0 && Rev3[var] == 0) {
Distancias[var] = Distancias[KKK] + 1;
COLA.push(var);
Rev3[var] = 1;
}
}
}
}
}
int MM, AA, BB;
scanf("%d", &MM);
for (int I = 0; I < MM; I++) {
scanf("%d%d", &AA, &BB);
AA--;
BB--;
if (Grafo[AA] != Grafo[BB] || AA == BB) {
printf("No\n");
} else {
if (CC[AA] != CC[BB])
printf("Yes\n");
else {
if ((Distancias[AA] + Distancias[BB] + 1) % 2 == 0) {
printf("Yes\n");
} else
printf("No\n");
}
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
inline int gi() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * f;
}
int fir[100010], dis[400010], nxt[400010], id;
inline void link(int a, int b) { nxt[++id] = fir[a], fir[a] = id, dis[id] = b; }
int dep[100010], st[17][100010], tot[100010], rt[100010];
inline void dfs(int x) {
for (int i = fir[x]; i; i = nxt[i])
if (!dep[dis[i]])
st[0][dis[i]] = x, rt[dis[i]] = rt[x], dep[dis[i]] = dep[x] + 1,
dfs(dis[i]);
}
inline int LCA(int x, int y) {
if (dep[x] < dep[y]) std::swap(x, y);
for (int i = 16; ~i; --i)
if (dep[st[i][x]] >= dep[y]) x = st[i][x];
for (int i = 16; ~i; --i)
if (st[i][x] != st[i][y]) x = st[i][x], y = st[i][y];
if (x != y) x = st[0][x];
return x;
}
inline void dfs2(int x) {
for (int i = fir[x]; i; i = nxt[i]) {
if (st[0][dis[i]] != x) continue;
tot[dis[i]] += tot[x];
dfs2(dis[i]);
}
}
int dfn[100010], low[100010], stk[100010], tp, ins[100010], scc[100010],
yes[100010], totscc;
inline void tarjan(int x, int fa = -1) {
dfn[x] = low[x] = ++dfn[0];
stk[++tp] = x;
ins[x] = 1;
for (int i = fir[x]; i; i = nxt[i]) {
if (dis[i] == fa) continue;
if (!dfn[dis[i]])
tarjan(dis[i], x), low[x] = std::min(low[x], low[dis[i]]);
else
low[x] = std::min(low[x], dfn[dis[i]]);
}
if (dfn[x] == low[x]) {
++totscc;
while (stk[tp + 1] != x) ins[stk[tp]] = 0, scc[stk[tp]] = totscc, --tp;
}
}
int main() {
int n = gi(), m = gi(), a, b;
for (int i = 1; i <= m; ++i) a = gi(), b = gi(), link(a, b), link(b, a);
for (int i = 1; i <= n; ++i)
if (!dep[i]) dep[i] = 1, rt[i] = i, dfs(i);
for (int i = 1; i < 17; ++i)
for (int j = 1; j <= n; ++j) st[i][j] = st[i - 1][st[i - 1][j]];
for (int i = 1; i <= n; ++i)
if (dep[i] == 1) tarjan(i);
for (int x = 1; x <= n; ++x) {
for (int i = fir[x]; i; i = nxt[i]) {
if ((dep[x] + dep[dis[i]]) & 1) continue;
if (x >= dis[i]) continue;
if (scc[x] != scc[dis[i]]) continue;
yes[scc[x]] = 1;
}
}
for (int x = 1; x <= n; ++x)
if (st[0][x] && scc[x] == scc[st[0][x]] && yes[scc[x]]) ++tot[x];
for (int i = 1; i <= n; ++i)
if (dep[i] == 1) dfs2(i);
int Q = gi(), x, y, lca;
while (Q--) {
x = gi(), y = gi();
if (rt[x] != rt[y] || x == y) {
puts("No");
continue;
}
lca = LCA(x, y);
if (tot[x] + tot[y] - 2 * tot[lca] || (dep[x] + dep[y]) & 1)
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100005, maxm = 100005;
struct edge {
int to, next;
} e[maxm * 2];
int h[maxn], tot;
int n, m, q;
struct Edge {
int u, v;
bool operator<(const Edge &a) const { return u != a.u ? u < a.u : v < a.v; }
};
stack<Edge> S;
vector<Edge> bcc_e[maxn];
map<Edge, int> odd;
inline void add(int u, int v) {
e[++tot] = (edge){v, h[u]};
h[u] = tot;
}
int Time, dfn[maxn], low[maxn], bcc_cnt, bccno[maxn];
vector<int> bcc[maxn];
bool iscut[maxn];
void tarjan(int u, int fa) {
dfn[u] = low[u] = ++Time;
int child = 0;
for (int i = h[u]; i; i = e[i].next) {
int v = e[i].to;
Edge E = (Edge){u, v};
if (!dfn[v]) {
S.push(E);
++child;
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
iscut[u] = true;
bcc_cnt++;
for (;;) {
Edge x = S.top();
S.pop();
bcc_e[bcc_cnt].push_back(x);
if (bccno[x.u] != bcc_cnt)
bcc[bcc_cnt].push_back(x.u), bccno[x.u] = bcc_cnt;
if (bccno[x.v] != bcc_cnt)
bcc[bcc_cnt].push_back(x.v), bccno[x.v] = bcc_cnt;
if (x.u == u && x.v == v) break;
}
}
} else if (dfn[v] < dfn[u] && v != fa) {
S.push(E);
low[u] = min(low[u], dfn[v]);
}
}
if (fa == 0 && child == 1) iscut[u] = false;
}
void find_bcc() {
for (int i = 1; i <= n; ++i)
if (!dfn[i]) tarjan(i, 0);
}
int now, col[maxn];
bool color(int u) {
for (int i = h[u]; i; i = e[i].next) {
int v = e[i].to;
if (bccno[v] != now) continue;
if (col[v] == col[u]) return false;
if (!col[v]) {
col[v] = 3 - col[u];
if (!color(v)) return false;
}
}
return true;
}
void color_bcc() {
fill(bccno + 1, bccno + n + 1, 0);
for (int i = 1; i <= bcc_cnt; ++i) {
for (int v : bcc[i]) bccno[v] = i;
int u = bcc[i][0];
col[u] = 1;
now = i;
if (!color(u))
for (Edge E : bcc_e[i]) odd[E] = true;
for (int v : bcc[i]) col[v] = 0;
}
}
int ufs[maxn], U[maxn], V[maxn];
int find(int x) {
if (ufs[x] == 0) return x;
return ufs[x] = find(ufs[x]);
}
bool merge(int u, int v) {
int x = find(u), y = find(v);
if (x == y) return false;
ufs[x] = y;
return true;
}
void build_tree() {
for (int i = 1, j = 1; i < n; ++i) {
while (j <= m && !merge(U[j], V[j])) ++j;
if (j > m) break;
add(U[j], V[j]);
add(V[j], U[j]);
}
}
int logn = 1;
int deep[maxn], f[maxn][20], unv[maxn][20];
int flag[maxn];
void dfs(int u, int fa) {
flag[u] = now;
f[u][0] = fa;
deep[u] = deep[fa] + 1;
for (int i = h[u], v; i; i = e[i].next) {
v = e[i].to;
if (v == fa) continue;
unv[v][0] = odd.count((Edge){u, v}) | odd.count((Edge){v, u});
dfs(v, u);
}
}
void get_fa() {
for (int &j = logn; (1 << j) <= n; ++j)
for (int i = 1; i <= n; ++i)
f[i][j] = f[f[i][j - 1]][j - 1],
unv[i][j] = unv[i][j - 1] | unv[f[i][j - 1]][j - 1];
}
bool lca(int u, int v) {
if (deep[u] > deep[v]) swap(u, v);
int ans = 0, p = deep[v] - deep[u], len = 0;
for (int i = 0; (1 << i) <= n; ++i)
if (p & (1 << i)) ans |= unv[v][i], v = f[v][i], len += (1 << i);
if (u == v) return ans || (len & 1);
for (int i = logn; i >= 0; --i)
if (f[u][i] != f[v][i]) {
ans |= unv[v][i] | unv[u][i];
u = f[u][i];
v = f[v][i];
len += (1 << i) << 1;
}
ans |= unv[v][0] | unv[u][0];
len += 2;
return ans || (len & 1);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &U[i], &V[i]);
add(U[i], V[i]);
add(V[i], U[i]);
}
find_bcc();
color_bcc();
fill(h + 1, h + n + 1, 0);
tot = 0;
build_tree();
now = 0;
for (int i = 1; i <= n; ++i)
if (!flag[i]) ++now, dfs(i, 0);
get_fa();
scanf("%d", &q);
for (int x, y, i = 1; i <= q; ++i) {
scanf("%d%d", &x, &y);
if (flag[x] == flag[y] && lca(x, y))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int Inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3fll;
struct DSU {
int fa[1 << 17];
DSU() {
for (int i = 0; i < (1 << 17); i++) fa[i] = i;
}
int inline root(int x) { return fa[x] == x ? x : (fa[x] = root(fa[x])); }
} dsu;
int n, m, q;
vector<int> nei[1 << 17];
int ceng[1 << 17];
int fa[20][1 << 17];
int hv[1 << 17], qzh[1 << 17];
void inline dfs(int now) {
ceng[now] = ceng[fa[0][now]] + 1;
for (__typeof((nei[now]).begin()) i = (nei[now]).begin(),
_e_D_ = (nei[now]).end();
i != _e_D_; i++) {
if (!ceng[*i]) {
fa[0][*i] = now;
dfs(*i);
if (dsu.root(*i) == dsu.root(now)) hv[now] |= hv[*i];
} else if (ceng[*i] + 1 < ceng[now]) {
if (!((ceng[now] ^ ceng[*i]) & 1)) hv[now] = 1;
for (int j = dsu.root(now); ceng[j] > ceng[*i] + 1; j = dsu.root(j))
dsu.fa[j] = fa[0][j];
}
}
}
void inline dfs2(int now) {
qzh[now] += hv[now];
for (__typeof((nei[now]).begin()) i = (nei[now]).begin(),
_e_D_ = (nei[now]).end();
i != _e_D_; i++)
if (ceng[*i] == ceng[now] + 1) {
if (dsu.root(*i) == dsu.root(now)) hv[*i] |= hv[now];
qzh[*i] = qzh[now];
dfs2(*i);
}
}
int inline lca(int a, int b) {
if (ceng[a] > ceng[b]) swap(a, b);
int toup = ceng[b] - ceng[a];
while (toup) b = fa[__builtin_ctz(toup)][b], toup &= (toup - 1);
if (a == b) return a;
for (int i = (19); ((-1) > 0 ? i <= (0) : i >= (0)); i += (-1))
if (fa[i][a] != fa[i][b]) a = fa[i][a], b = fa[i][b];
return fa[0][a];
}
bool inline query(int a, int b) {
int c = lca(a, b);
if (!c) return 0;
if ((ceng[a] ^ ceng[b]) & 1)
return 1;
else
return qzh[a] + qzh[b] - 2 * qzh[c];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= (m); i++) {
int a, b;
scanf("%d%d", &a, &b);
nei[a].push_back(b);
nei[b].push_back(a);
}
for (int i = 1; i <= (n); i++)
if (!ceng[i]) dfs(i), dfs2(i);
for (int i = 1; i <= (19); i++)
for (int j = 1; j <= (n); j++) fa[i][j] = fa[i - 1][fa[i - 1][j]];
scanf("%d", &q);
while (q--) {
int a, b;
scanf("%d%d", &a, &b);
puts(query(a, b) ? "Yes" : "No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 110000;
const int INF = 1e9;
struct dsu {
vector<int> lab;
vector<bool> odd;
void init(int n) {
lab.resize(n + 1);
for (int i = 0; i <= n; i++) lab[i] = i;
odd.resize(n + 1, false);
}
int find(int v) {
if (lab[v] == v) return v;
return lab[v] = find(lab[v]);
}
bool disp(int v) { return odd[find(v)]; }
bool same(int u, int v) { return find(u) == find(v); }
void unis(int u, int v, bool d) {
if (d) odd[find(u)] = true;
if (same(u, v)) return;
odd[find(u)] = odd[find(u)] || odd[find(v)];
lab[find(v)] = find(u);
}
};
int n, m;
vector<int> adj[MAXN];
int root[MAXN], parent[MAXN], liv[MAXN];
int primo[MAXN];
dsu uf;
void dfs(int v, int p, int r, int d) {
if (liv[v]) return;
root[v] = r;
parent[v] = p;
liv[v] = d;
for (int i = 0; i < (int)adj[v].size(); i++) {
dfs(adj[v][i], v, r, d + 1);
}
}
void dfs2(int v, int f) {
if (primo[v]) return;
primo[v] = f;
if (uf.disp(v)) primo[v] = v;
for (int i = 0; i < (int)adj[v].size(); i++) {
dfs2(adj[v][i], primo[v]);
}
}
int main() {
ios::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= n; i++) dfs(i, 0, i, 1);
uf.init(n);
for (int v = 1; v <= n; v++)
for (int j = 0; j < (int)adj[v].size(); j++) {
int u = adj[v][j];
if (liv[u] > liv[v]) continue;
if (liv[v] == liv[u] + 1) {
assert(parent[v] == u);
continue;
}
assert(liv[v] > liv[u]);
bool dis = false;
if (liv[v] % 2 == liv[u] % 2) dis = true;
int x = v;
while (liv[x] > liv[u]) {
uf.unis(x, v, dis);
x = parent[uf.find(x)];
}
}
for (int i = 1; i <= n; i++) dfs2(i, 1);
int q;
for (cin >> q; q > 0; q--) {
int a, b;
cin >> a >> b;
if (root[a] != root[b]) {
cout << "No\n";
continue;
}
if (liv[a] % 2 != liv[b] % 2) {
cout << "Yes\n";
continue;
}
if (primo[a] != primo[b]) {
cout << "Yes\n";
continue;
}
cout << "No\n";
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class T>
void rd(T &x) {
x = 0;
int f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
x *= f;
}
const int maxn = 2e5 + 5;
int n, m, top, idx, bcc, q;
int _u[maxn], _v[maxn];
int dfn[maxn], low[maxn], sta[maxn];
int dep[maxn], pre[maxn][21], mrk[maxn][21];
struct edge {
int v, nxt;
};
vector<edge> e;
struct round_square_tree {
int head[maxn];
round_square_tree() { memset(head, -1, sizeof(head)); }
void adde(int u, int v) {
e.push_back((edge){v, head[u]});
head[u] = e.size() - 1;
e.push_back((edge){u, head[v]});
head[v] = e.size() - 1;
}
void dfs(int u, int f) {
dep[u] = dep[f] + 1;
pre[u][0] = f;
for (int i = (1); i <= (20); ++i) pre[u][i] = pre[pre[u][i - 1]][i - 1];
for (int i = head[u]; ~i; i = e[i].nxt) {
int v = e[i].v;
if (v == f) continue;
dfs(v, u);
}
}
void dfs2(int u, int f) {
for (int i = (1); i <= (20); ++i)
mrk[u][i] = mrk[u][i - 1] | mrk[pre[u][i - 1]][i - 1];
for (int i = head[u]; ~i; i = e[i].nxt)
if (e[i].v != f) dfs2(e[i].v, u);
}
bool check(int a, int b) {
bool res = 0;
if (dep[a] > dep[b]) swap(a, b);
for (int i = (20); i >= (0); --i)
if (dep[a] <= dep[b] - (1 << i)) res |= mrk[b][i], b = pre[b][i];
if (a == b) return res | mrk[a][0];
for (int i = (20); i >= (0); --i)
if (pre[a][i] != pre[b][i])
res |= mrk[a][i] | mrk[b][i], a = pre[a][i], b = pre[b][i];
return res | mrk[a][1] | mrk[b][1];
}
} rst;
struct graph {
int head[maxn];
graph() { memset(head, -1, sizeof(head)); }
void adde(int u, int v) {
e.push_back((edge){v, head[u]});
head[u] = e.size() - 1;
e.push_back((edge){u, head[v]});
head[v] = e.size() - 1;
}
void tarjan(int u) {
dfn[u] = low[u] = ++idx;
sta[++top] = u;
for (int i = head[u]; ~i; i = e[i].nxt) {
int v = e[i].v;
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
if (low[v] == dfn[u]) {
int t = -1;
bcc++;
while (t != v) {
t = sta[top--];
rst.adde(bcc, t);
}
rst.adde(bcc, u);
}
} else
low[u] = min(low[u], dfn[v]);
}
}
} G;
struct union_find_set {
int fa[maxn], xr[maxn];
int find(int u) {
if (u == fa[u]) return u;
int f = fa[u];
fa[u] = find(fa[u]);
xr[u] ^= xr[f];
return fa[u];
}
void work() {
for (int i = (1); i <= (n); ++i) fa[i] = i;
for (int i = (1); i <= (m); ++i) {
int u = _u[i];
int v = _v[i];
int fu = find(u);
int fv = find(v);
if (fu == fv) {
if (xr[u] == xr[v]) {
if (dep[u] > dep[v]) swap(u, v);
mrk[pre[v][0]][0] = 1;
}
} else
fa[fu] = fv, xr[fu] = xr[u] ^ xr[v] ^ 1;
}
}
} ufs;
vector<int> S;
void sol() {
for (int i = (1); i <= (n); ++i)
if (!dfn[i]) G.tarjan(i);
for (int i = (1); i <= (n); ++i)
if (!dep[i]) S.push_back(i), rst.dfs(i, 0);
ufs.work();
for (int i = (0); i <= ((int)S.size() - 1); ++i) rst.dfs2(S[i], 0);
rd(q);
while (q--) {
int u, v;
rd(u), rd(v);
if (ufs.find(u) != ufs.find(v)) {
puts("No");
continue;
}
if (ufs.xr[u] != ufs.xr[v])
puts("Yes");
else if (rst.check(u, v))
puts("Yes");
else
puts("No");
}
}
int main() {
rd(n), rd(m);
bcc = n;
for (int i = (1); i <= (m); ++i) {
rd(_u[i]), rd(_v[i]);
G.adde(_u[i], _v[i]);
}
sol();
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
void read(int &x) {
char c = getchar();
x = 0;
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
}
const int maxn = 2e5 + 10;
int n, m;
namespace Tree {
int tote = 1, FIR[maxn], TO[maxn], NEXT[maxn];
int fa[maxn], qf[20][maxn], dep[maxn];
int c, rmq[20][maxn * 2], lg[maxn * 2];
bool vis[maxn], col[maxn];
int cnt[maxn], sum[maxn], fir[maxn];
int f[maxn];
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
void addedge(int u, int v) {
TO[++tote] = v;
NEXT[tote] = FIR[u];
FIR[u] = tote;
}
void rmq_build() {
int i, j;
for (i = 0; 1 << i <= c; i++) lg[1 << i] = i;
for (i = 1; i <= c; i++) lg[i] = max(lg[i], lg[i - 1]);
for (i = 1; i <= lg[c]; i++)
for (j = 1; j <= c - (1 << i) + 1; j++) {
int l = rmq[i - 1][j], r = rmq[i - 1][j + (1 << (i - 1))];
rmq[i][j] = dep[l] < dep[r] ? l : r;
}
}
int lca(int u, int v) {
u = fir[u];
v = fir[v];
if (u > v) swap(u, v);
int sz = lg[v - u];
int l = rmq[sz][u], r = rmq[sz][v - (1 << sz) + 1];
return dep[l] < dep[r] ? l : r;
}
void dfs(int u) {
qf[0][u] = fa[u];
for (int i = 1; i < 20; i++) qf[i][u] = qf[i - 1][qf[i - 1][u]];
vis[u] = 1;
rmq[0][++c] = u;
fir[u] = c;
for (int p = FIR[u]; p; p = NEXT[p]) {
int v = TO[p];
if (!vis[v]) {
fa[v] = u;
dep[v] = dep[u] + 1;
col[v] = !col[u];
dfs(v);
rmq[0][++c] = u;
}
}
}
int q[maxn], H = 0, T = 0;
void bfs(int S) {
q[++T] = S;
while (H - T)
for (int p = FIR[q[++H]]; p; p = NEXT[p])
if (fa[TO[p]] == q[H]) q[++T] = TO[p];
}
void build() {
int i;
for (i = 1; i <= n; i++)
if (!vis[i]) {
rmq[0][++c] = 0;
dep[i] = 1;
dfs(i);
bfs(i);
}
rmq_build();
}
void prework() {
int i, j;
for (i = 1; i <= n; i++) f[i] = i;
for (i = 1; i <= m; i++) {
int u = TO[i << 1], v = TO[i << 1 | 1], c;
if (dep[u] < dep[v]) swap(u, v);
if (fa[u] == v) continue;
if (find(u) == find(v)) continue;
for (c = u, j = 19; j >= 0; j--)
if (dep[qf[j][c]] > dep[v]) c = qf[j][c];
for (u = find(u); dep[u] > dep[v]; u = find(fa[u])) f[find(u)] = find(c);
}
for (i = 1; i <= m; i++) {
int u = TO[i << 1], v = TO[i << 1 | 1];
if (dep[u] < dep[v]) swap(u, v);
if (fa[u] == v) continue;
if (col[u] == col[v]) cnt[find(u)] = 1;
}
for (i = 1; i <= n; i++) cnt[i] = cnt[find(i)];
for (i = 1; i <= n; i++) sum[q[i]] = sum[fa[q[i]]] + cnt[q[i]];
}
bool query(int u, int v) {
int f = lca(u, v);
if (!f) return 0;
if (col[u] ^ col[v]) return 1;
return sum[u] + sum[v] - sum[f] - sum[f];
}
} // namespace Tree
int main() {
int i, u, v;
read(n);
read(m);
for (i = 1; i <= m; i++) {
read(u);
read(v);
Tree::addedge(u, v);
Tree::addedge(v, u);
}
Tree::build();
Tree::prework();
read(m);
while (m--) {
read(u);
read(v);
puts(Tree::query(u, v) ? "Yes" : "No");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long n, m, q, fa[200010], dep[200010], dep2[200010], f2[200010][25],
f[200010][25], lw[200010], din[200010], par[200010],
cnt = 0, cnt2 = 0, sum[200010], col[200010];
bool vis[200010], odd[200010];
vector<long long> sz[200010], vt[200010], vt2[200010];
stack<long long> stk;
long long getf(long long x) {
if (x == fa[x]) {
return x;
}
return fa[x] = getf(fa[x]);
}
void dfs(long long x, long long lst, long long d) {
vis[x] = true;
lw[x] = din[x] = ++cnt;
dep[x] = d;
long long i;
for (i = 0; i < 22; i++) {
f[x][i + 1] = f[f[x][i]][i];
}
for (i = 0; i < vt[x].size(); i++) {
if (vt[x][i] != lst) {
if (vis[vt[x][i]]) {
lw[x] = min(lw[x], din[vt[x][i]]);
} else {
stk.push(vt[x][i]);
f[vt[x][i]][0] = x;
dfs(vt[x][i], x, d + 1);
lw[x] = min(lw[x], lw[vt[x][i]]);
if (lw[vt[x][i]] >= din[x]) {
while (stk.top() != vt[x][i]) {
sz[cnt2].push_back(stk.top());
stk.pop();
}
sz[cnt2].push_back(stk.top());
stk.pop();
sz[cnt2++].push_back(x);
}
}
}
}
return;
}
void dfs2(long long x, long long lst, long long d) {
vis[x] = true;
par[x] = lst;
if (odd[x]) {
sum[x] = 1;
} else {
sum[x] = 0;
}
if (lst != -1) {
sum[x] += sum[lst];
}
dep2[x] = d;
long long i;
for (i = 0; i < 22; i++) {
f2[x][i + 1] = f2[f2[x][i]][i];
}
for (i = 0; i < vt2[x].size(); i++) {
if (!vis[vt2[x][i]]) {
f2[vt2[x][i]][0] = x;
dfs2(vt2[x][i], x, d + 1);
}
}
return;
}
long long glca(long long x, long long y) {
long long i;
if (dep[x] < dep[y]) {
swap(x, y);
}
for (i = 22; i >= 0; i--) {
if (dep[f[x][i]] >= dep[y]) {
x = f[x][i];
}
if (x == y) {
return x;
}
}
for (i = 22; i >= 0; i--) {
if (f[x][i] != f[y][i]) {
x = f[x][i];
y = f[y][i];
}
}
return f[x][0];
}
long long glca2(long long x, long long y) {
long long i;
if (dep2[x] < dep2[y]) {
swap(x, y);
}
for (i = 22; i >= 0; i--) {
if (dep2[f2[x][i]] >= dep2[y]) {
x = f2[x][i];
}
if (x == y) {
return x;
}
}
for (i = 22; i >= 0; i--) {
if (f2[x][i] != f2[y][i]) {
x = f2[x][i];
y = f2[y][i];
}
}
return f2[x][0];
}
void fd(long long id) {
long long i, x, p;
queue<long long> q;
for (i = 0; i < sz[id].size(); i++) {
vis[sz[id][i]] = false;
}
odd[id + n] = false;
col[sz[id][0]] = 0;
vis[sz[id][0]] = true;
q.push(sz[id][0]);
while (!q.empty()) {
x = q.front();
q.pop();
for (i = 0; i < vt[x].size(); i++) {
p = lower_bound(sz[id].begin(), sz[id].end(), vt[x][i]) - sz[id].begin();
if (p >= sz[id].size() || sz[id][p] != vt[x][i]) {
continue;
}
if (!vis[vt[x][i]]) {
col[vt[x][i]] = col[x] ^ 1;
vis[vt[x][i]] = true;
q.push(vt[x][i]);
} else if (col[vt[x][i]] == col[x]) {
odd[id + n] = true;
return;
}
}
}
return;
}
int main() {
long long i, j, x, y, a, b;
scanf("%lld%lld", &n, &m);
for (i = 0; i < n; i++) {
fa[i] = i;
}
for (i = 0; i < m; i++) {
scanf("%lld%lld", &x, &y);
x--;
y--;
vt[x].push_back(y);
vt[y].push_back(x);
a = getf(x);
b = getf(y);
fa[b] = a;
}
for (i = 0; i < n; i++) {
fa[i] = getf(i);
}
memset(vis, false, sizeof(vis));
for (i = 0; i < n; i++) {
if (!vis[i]) {
dfs(i, -1, 9);
}
}
for (i = 0; i < cnt2; i++) {
sort(sz[i].begin(), sz[i].end());
fd(i);
for (j = 0; j < sz[i].size(); j++) {
vt2[sz[i][j]].push_back(i + n);
vt2[i + n].push_back(sz[i][j]);
}
}
memset(vis, false, sizeof(vis));
memset(sum, 0, sizeof(sum));
for (i = 0; i < n + cnt2; i++) {
if (!vis[i]) {
dfs2(i, -1, 0);
}
}
scanf("%lld", &q);
while (q--) {
scanf("%lld%lld", &x, &y);
x--;
y--;
if (fa[x] != fa[y] || x == y) {
puts("No");
continue;
}
a = glca(x, y);
b = glca2(x, y);
b = par[b];
if (b == -1) {
if (sum[y] != 0 || sum[x] != 0) {
puts("Yes");
continue;
}
} else {
if (sum[y] - sum[b] != 0) {
puts("Yes");
continue;
} else if (sum[x] - sum[b] != 0) {
puts("Yes");
continue;
}
}
if ((dep[x] + dep[y] - dep[a] * 2) % 2 == 1) {
puts("Yes");
} else {
puts("No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chkmax(T& a, T b) {
return a < b ? a = b, 1 : 0;
}
template <typename T>
bool chkmin(T& a, T b) {
return a > b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
const int maxn = 200000 + 5;
template <typename T>
T read() {
T n(0), f(1);
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') f = -1;
for (; isdigit(ch); ch = getchar()) n = n * 10 + ch - 48;
return n * f;
}
vector<int> G[maxn];
void addedge(int u, int v) {
G[u].push_back(v);
G[v].push_back(u);
}
int s[maxn], n, m, q;
int fa[maxn][20], dep[maxn];
int dfn[maxn], low[maxn], dfs_clock;
int pa[maxn];
int findset(int x) { return x == pa[x] ? x : pa[x] = findset(pa[x]); }
int top;
pair<int, int> stk[maxn];
void dfs(int u, int f = 0) {
fa[u][0] = f;
pa[u] = f ? f : u;
dep[u] = dep[f] + 1;
low[u] = dfn[u] = ++dfs_clock;
for (int i = 1; i < 20; ++i) {
if (!fa[u][i - 1]) break;
fa[u][i] = fa[fa[u][i - 1]][i - 1];
}
for (const auto& v : G[u]) {
pair<int, int> cur = pair<int, int>(u, v);
if (!dfn[v]) {
stk[top++] = cur;
dfs(v, u);
chkmin(low[u], low[v]);
if (low[v] >= dfn[u]) {
bool flag = 0;
for (int i = top - 1;; --i) {
if ((dep[stk[i].first] & 1) == (dep[stk[i].second] & 1)) {
flag = 1;
break;
}
if (stk[i] == cur) break;
}
do {
if (stk[top - 1].first != u) s[stk[top - 1].first] = flag;
if (stk[top - 1].second != u) s[stk[top - 1].second] = flag;
} while (stk[--top] != cur);
}
} else if (v != f && dfn[v] < dfn[u]) {
stk[top++] = cur;
chkmin(low[u], dfn[v]);
}
}
}
void calc(int u, int f = 0) {
s[u] += s[f];
for (const auto& v : G[u])
if (fa[v][0] == u) {
calc(v, u);
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
if (dep[u] > dep[v])
for (int i = 19; i >= 0; --i) {
if (dep[fa[u][i]] >= dep[v]) u = fa[u][i];
}
if (u == v) return u;
for (int i = 19; i >= 0; --i)
if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
return fa[u][0];
}
int main() {
n = read<int>();
m = read<int>();
for (int i = 0; i < m; ++i) {
static int u, v;
u = read<int>();
v = read<int>();
addedge(u, v);
}
for (int i = 1; i <= n; ++i)
if (!dfn[i]) dfs(i);
for (int i = 1; i <= n; ++i)
if (fa[i][0] == 0) calc(i);
q = read<int>();
while (q--) {
static int u, v;
u = read<int>(), v = read<int>();
if (findset(u) != findset(v)) {
puts("No");
continue;
}
if ((dep[u] % 2 != dep[v] % 2) || s[u] + s[v] - 2 * s[lca(u, v)] > 0)
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000;
int n, m;
unordered_set<int> g[MAXN];
int color[MAXN];
int low[MAXN];
int depth[MAXN];
stack<pair<int, int> > edge_stack;
vector<pair<int, int> > edges_to_remove;
int COMP_ID;
int comp_before[MAXN];
int comp_after[MAXN];
void remove_comp(int u, int v, bool odd) {
pair<int, int> uv(u, v);
while (true) {
pair<int, int> top = edge_stack.top();
edge_stack.pop();
if (odd) edges_to_remove.push_back(top);
if (top == uv) break;
}
}
bool dfs_before(int u, int p, int d) {
comp_before[u] = COMP_ID;
depth[u] = d;
color[u] = d % 2;
low[u] = d - 1;
bool u_odd = false;
for (int v : g[u]) {
if (v == p) continue;
if (depth[v] == -1) {
edge_stack.emplace(u, v);
bool v_odd = dfs_before(v, u, d + 1);
if (p == -1) {
assert(low[v] >= d);
}
if (low[v] >= d) {
remove_comp(u, v, v_odd);
} else {
u_odd = u_odd or v_odd;
low[u] = min(low[u], low[v]);
}
} else if (depth[v] < d) {
assert(d - depth[v] >= 2);
edge_stack.emplace(u, v);
low[u] = min(low[u], depth[v]);
if (color[u] == color[v]) {
u_odd = true;
}
} else {
}
}
return u_odd;
}
void dfs_after(int u, int p) {
comp_after[u] = COMP_ID;
for (int v : g[u]) {
if (v == p or comp_after[v] != -1) continue;
dfs_after(v, u);
}
}
int main() {
scanf("%d%d", &n, &m);
while (m--) {
int a, b;
scanf("%d%d", &a, &b);
--a, --b;
g[a].insert(b);
g[b].insert(a);
}
memset(depth, -1, sizeof(depth[0]) * n);
COMP_ID = 0;
for (int u = 0; u <= n - 1; ++u) {
if (depth[u] == -1) {
dfs_before(u, -1, 0);
COMP_ID++;
}
}
for (auto& e : edges_to_remove) {
g[e.first].erase(e.second);
g[e.second].erase(e.first);
}
memset(comp_after, -1, sizeof(comp_after[0]) * n);
for (int u = 0; u <= n - 1; ++u) {
if (comp_after[u] == -1) {
dfs_after(u, -1);
COMP_ID++;
}
}
int q;
scanf("%d", &q);
while (q--) {
int a, b;
scanf("%d%d", &a, &b);
--a, --b;
if (comp_before[a] != comp_before[b])
puts("No");
else if (comp_after[a] != comp_after[b])
puts("Yes");
else if (color[a] != color[b])
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000;
int n, m;
unordered_set<int> g[MAXN];
int color[MAXN];
int low[MAXN];
int depth[MAXN];
stack<pair<int, int> > edge_stack;
vector<pair<int, int> > edges_to_remove;
int COMP_ID;
int comp_before[MAXN];
int comp_after[MAXN];
void remove_comp(int u, int v, bool odd) {
pair<int, int> uv(u, v);
while (true) {
pair<int, int> top = edge_stack.top();
edge_stack.pop();
if (odd) edges_to_remove.push_back(top);
if (top == uv) break;
}
}
bool dfs_before(int u, int p, int d) {
comp_before[u] = COMP_ID;
depth[u] = d;
color[u] = d % 2;
low[u] = d - 1;
bool u_odd = false;
for (int v : g[u]) {
if (v == p) continue;
if (depth[v] == -1) {
edge_stack.emplace(u, v);
bool v_odd = dfs_before(v, u, d + 1);
if (p == -1) {
assert(low[v] >= d);
}
if (low[v] >= d) {
remove_comp(u, v, v_odd);
} else {
u_odd = u_odd or v_odd;
low[u] = min(low[u], low[v]);
}
} else if (depth[v] < d) {
assert(d - depth[v] >= 2);
edge_stack.emplace(u, v);
low[u] = min(low[u], depth[v]);
if (color[u] == color[v]) {
u_odd = true;
}
} else {
}
}
return u_odd;
}
void dfs_after(int u, int p) {
comp_after[u] = COMP_ID;
for (int v : g[u]) {
if (v == p or comp_after[v] != -1) continue;
dfs_after(v, u);
}
}
int main() {
scanf("%d%d", &n, &m);
while (m--) {
int a, b;
scanf("%d%d", &a, &b);
--a, --b;
g[a].insert(b);
g[b].insert(a);
}
memset(depth, -1, sizeof(depth[0]) * n);
COMP_ID = 0;
for (int u = 0; u <= n - 1; ++u) {
if (depth[u] == -1) {
dfs_before(u, -1, 0);
COMP_ID++;
}
}
for (auto& e : edges_to_remove) {
g[e.first].erase(e.second);
g[e.second].erase(e.first);
}
memset(comp_after, -1, sizeof(comp_after[0]) * n);
for (int u = 0; u <= n - 1; ++u) {
if (comp_after[u] == -1) {
dfs_after(u, -1);
COMP_ID++;
}
}
int q;
scanf("%d", &q);
while (q--) {
int a, b;
scanf("%d%d", &a, &b);
--a, --b;
if (comp_before[a] != comp_before[b])
puts("No");
else if (comp_after[a] != comp_after[b])
puts("Yes");
else if (color[a] != color[b])
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxx = 1e5 + 20, mxk = 20;
stack<int> stk;
vector<int> cp[maxx], g[maxx];
vector<pair<int, int> > adj[maxx];
int zoj[maxx], in[maxx], pid[maxx], n, m, q, x,
co = 0, y, par[maxx][mxk], mh[maxx], dp[maxx][mxk], h[maxx];
bool vis[maxx], vism[maxx], mark[maxx];
pair<int, int> e[maxx];
bool fard;
void dfs2(int root, int c = 1) {
vis[root] = true;
zoj[root] = c;
for (int x : g[root]) {
if (!vis[x])
dfs2(x, 3 - c);
else if (zoj[x] != 3 - c)
fard = true;
}
}
void dfs(int root, int hh = 0, int pr = -1) {
h[root] = mh[root] = hh;
if (pr == -1) pr = root;
par[root][0] = pr;
vis[root] = true;
for (pair<int, int> e : adj[root]) {
int x = e.first, id = e.second;
if (!vis[x]) {
in[x] = id;
if (!vism[id]) stk.push(id), vism[id] = true;
pid[x] = id;
dfs(x, hh + 1, root), mh[root] = min(mh[root], mh[x]);
} else {
mh[root] = min(mh[root], h[x]);
if (!vism[id]) stk.push(id), vism[id] = true;
}
}
if (h[root] && mh[root] >= h[root] - 1) {
while (stk.top() != in[root]) cp[co].push_back(stk.top()), stk.pop();
cp[co++].push_back(stk.top()), stk.pop();
}
}
bool lca(int x, int y) {
if (par[x][19] != par[y][19]) return false;
int tool = 0;
bool ans = false;
if (h[x] > h[y]) swap(x, y);
int g = h[y] - h[x];
tool += g;
if (g % 2) return true;
for (int i = 0; i < mxk; i++)
if (g & (1 << i)) ans = (ans || dp[y][i]), y = par[y][i];
if (x == y) return (ans);
for (int i = mxk - 1; i >= 0; i--) {
if (par[x][i] != par[y][i])
ans = (ans || dp[x][i]), ans = (ans || dp[y][i]), x = par[x][i],
y = par[y][i];
}
ans = (ans || dp[x][0]);
ans = (ans || dp[y][0]);
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> x >> y;
x--, y--;
e[i] = {x, y};
adj[x].push_back({y, i});
adj[y].push_back({x, i});
}
for (int i = 0; i < n; i++)
if (!vis[i]) dfs(i);
for (int i = 1; i < mxk; i++) {
for (int j = 0; j < n; j++) par[j][i] = par[par[j][i - 1]][i - 1];
}
memset(vis, false, sizeof vis);
for (int i = 0; i < co; i++) {
for (int x : cp[i])
g[e[x].first].push_back(e[x].second),
g[e[x].second].push_back(e[x].first);
fard = false;
dfs2(e[cp[i][0]].first);
if (fard)
for (int x : cp[i]) mark[x] = true;
for (int x : cp[i])
g[e[x].first].clear(), g[e[x].second].clear(),
zoj[e[x].first] = 0, zoj[e[x].second] = 0, vis[e[x].first] = false,
vis[e[x].second] = false;
}
for (int i = 0; i < n; i++) {
if (par[i][0] == i)
dp[i][0] = false;
else
dp[i][0] = mark[pid[i]];
}
for (int i = 1; i < mxk; i++) {
for (int j = 0; j < n; j++)
dp[j][i] = (dp[j][i - 1] || dp[par[j][i - 1]][i - 1]);
}
cin >> q;
while (q--) {
cin >> x >> y;
x--, y--;
if (lca(x, y))
cout << "Yes" << '\n';
else
cout << "No" << '\n';
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int read() {
int kkk = 0, x = 1;
char c = getchar();
while ((c < '0' || c > '9') && c != '-') c = getchar();
if (c == '-') c = getchar(), x = -1;
while (c >= '0' && c <= '9') kkk = kkk * 10 + (c - '0'), c = getchar();
return kkk * x;
}
int N, n, m, q;
int vis[MAXN], deep[MAXN], jump[MAXN][22], R[MAXN], father[MAXN];
struct node {
int head[MAXN * 2], tot;
int to[MAXN * 2], nextn[MAXN * 2];
void ADD(int u, int v) {
to[++tot] = v, nextn[tot] = head[u];
head[u] = tot;
}
} Ed, T;
void format(int u, int fa) {
vis[u] = 1;
deep[u] = deep[fa] + 1;
int LOG = log2(deep[u]);
jump[u][0] = fa;
for (int i = 1; i <= LOG; ++i) jump[u][i] = jump[jump[u][i - 1]][i - 1];
for (int i = Ed.head[u]; i != 0; i = Ed.nextn[i]) {
int v = Ed.to[i];
if (vis[v]) continue;
format(v, u);
}
}
int LCA(int x, int y) {
if (deep[x] < deep[y]) swap(x, y);
int C = deep[x] - deep[y], LOG = log2(C);
for (int i = 0; i <= LOG; ++i)
if (C & (1 << i)) x = jump[x][i];
if (x == y) return x;
LOG = log2(deep[x]);
for (int i = LOG; i >= 0; --i)
if (jump[x][i] != jump[y][i]) x = jump[x][i], y = jump[y][i];
return jump[x][0];
}
int dfn[MAXN], low[MAXN], tim, z[MAXN], top;
vector<int> P[MAXN * 2];
vector<pair<int, int>> E[MAXN * 2];
void tarjan(int u, int from) {
dfn[u] = low[u] = ++tim;
z[++top] = u;
for (int i = Ed.head[u]; i != 0; i = Ed.nextn[i])
if (i != from) {
int v = Ed.to[i];
if (!dfn[v]) {
tarjan(v, i ^ 1);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
++N;
T.ADD(u, N);
P[N].push_back(u);
int x;
do {
x = z[top--];
T.ADD(N, x);
P[N].push_back(x);
} while (x != v);
}
} else
low[u] = min(low[u], dfn[v]);
}
}
int Deep[MAXN * 2], Jump[MAXN * 2][23], sum[MAXN * 2];
void Format(int u, int fa) {
Deep[u] = Deep[fa] + 1;
int LOG = log2(Deep[u]);
Jump[u][0] = fa;
for (int i = 1; i <= LOG; ++i) Jump[u][i] = Jump[Jump[u][i - 1]][i - 1];
for (int i = T.head[u]; i != 0; i = T.nextn[i]) {
int v = T.to[i];
Format(v, u);
}
}
int TLCA(int x, int y) {
if (Deep[x] < Deep[y]) swap(x, y);
int C = Deep[x] - Deep[y], LOG = log2(C);
for (int i = 0; i <= LOG; ++i)
if (C & (1 << i)) x = Jump[x][i];
if (x == y) return x;
LOG = log2(Deep[x]);
for (int i = LOG; i >= 0; --i)
if (Jump[x][i] != Jump[y][i]) x = Jump[x][i], y = Jump[y][i];
return Jump[x][0];
}
int col[MAXN], flag;
void paint(int u) {
for (int i = Ed.head[u]; i != 0; i = Ed.nextn[i]) {
int v = Ed.to[i];
if (col[v] == -1) {
col[v] = col[u] ^ 1;
paint(v);
} else if (col[u] == col[v])
flag = 1;
}
}
void Push(int u) {
for (int i = T.head[u]; i != 0; i = T.nextn[i]) {
int v = T.to[i];
sum[v] += sum[u];
Push(v);
}
}
int find(int k) {
if (father[k] != k) father[k] = find(father[k]);
return father[k];
}
int main() {
N = n = read(), m = read();
Ed.tot = 1;
for (int i = 1; i <= n; ++i) father[i] = i;
for (int i = 1; i <= m; ++i) {
int u = read(), v = read();
if (find(u) != find(v)) father[find(u)] = find(v);
Ed.ADD(u, v);
Ed.ADD(v, u);
}
for (int i = 1; i <= n; ++i)
if (!deep[i]) format(i, 0);
for (int i = 1; i <= n; ++i)
if (!dfn[i]) tarjan(i, 0), R[i] = 1;
for (int i = 1; i <= n; ++i)
if (R[i]) Format(i, 0);
for (int u = 1; u <= n; ++u) {
for (int i = Ed.head[u]; i != 0; i = Ed.nextn[i]) {
int v = Ed.to[i], tmp = u;
if (u > v) continue;
if (Deep[tmp] < Deep[v]) swap(tmp, v);
E[Jump[tmp][0]].push_back(make_pair(tmp, v));
}
}
for (int p = n + 1; p <= N; ++p) {
for (int i = 0; i < P[p].size(); ++i)
col[P[p][i]] = -1, Ed.head[P[p][i]] = 0;
Ed.tot = flag = 0;
for (int i = 0; i < E[p].size(); ++i) {
Ed.ADD(E[p][i].first, E[p][i].second);
Ed.ADD(E[p][i].second, E[p][i].first);
}
col[P[p][0]] = 0;
paint(P[p][0]);
sum[p] = flag;
}
for (int i = 1; i <= n; ++i)
if (R[i]) Push(i);
q = read();
int QQ = 0;
while (q--) {
int u = read(), v = read();
++QQ;
if (find(u) != find(v)) {
puts("No");
continue;
}
int dis = deep[u] + deep[v] - 2 * deep[LCA(u, v)];
if (dis & 1)
puts("Yes");
else {
dis = sum[u] + sum[v] - sum[TLCA(u, v)] - sum[Jump[TLCA(u, v)][0]];
if (dis)
puts("Yes");
else
puts("No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using i3 = pair<int, ii>;
using li = pair<ll, int>;
using lii = pair<ll, ii>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vii = vector<ii>;
using vli = vector<li>;
using vpll = vector<pll>;
using vi3 = vector<i3>;
using vlii = vector<lii>;
const int N = 4e5 + 5, L = 20;
const ll INF = 1e17 + 7;
const double eps = 1e-9, PI = acos(-1);
int n, m, q;
vii adj[N];
vi g[N], tmp[N];
int low[N], dfn[N], timer = 0;
vi edgesStack;
int cool;
int color[N];
ii edges[N];
bool fucked[N];
void chk(int u, int c) {
color[u] = c;
for (int v : tmp[u]) {
if (color[v] == 0)
chk(v, c == 1 ? 2 : 1);
else if (color[u] == color[v])
cool = 0;
}
}
void processBlock(int E) {
if (edgesStack.empty()) return;
set<int> currEdges;
set<int> nodes;
while (!edgesStack.empty()) {
int E2 = edgesStack.back();
edgesStack.pop_back();
int u = edges[E2].first;
int v = edges[E2].second;
tmp[u].push_back(v);
tmp[v].push_back(u);
nodes.insert(u);
nodes.insert(v);
currEdges.insert(E2);
if (E2 == E) break;
}
cool = 1;
chk(*nodes.begin(), 1);
if (!cool) {
for (int E : currEdges) fucked[E] = 1;
}
for (int u : nodes) {
tmp[u].clear();
color[u] = 0;
}
}
void dfs(int u, int p) {
dfn[u] = low[u] = ++timer;
int retchel = 0;
for (ii A : adj[u]) {
int v = A.second;
int e = A.first;
if (!dfn[v]) {
edgesStack.push_back(e);
retchel++;
dfs(v, u);
low[u] = min(low[u], low[v]);
if ((p == -1 && retchel > 1) || (p != -1 && dfn[u] <= low[v])) {
processBlock(e);
}
} else if (v != p) {
low[u] = min(low[u], dfn[v]);
if (dfn[v] < dfn[u]) {
edgesStack.push_back(e);
}
}
}
}
int p[N];
int p2[N];
int get(int x) { return x == p[x] ? x : p[x] = get(p[x]); }
void join(int x, int y) {
if ((x = get(x)) == (y = get(y))) return;
if (rand() & 1) swap(x, y);
p[x] = y;
}
int get2(int x) { return x == p2[x] ? x : p2[x] = get2(p2[x]); }
void join2(int x, int y) {
if ((x = get2(x)) == (y = get2(y))) return;
if (rand() & 1) swap(x, y);
p2[x] = y;
}
void dfsColor(int u, int c) {
color[u] = c;
for (int v : g[u]) {
if (color[v] == -1) dfsColor(v, 1 - c);
}
}
void solve(int testCase) {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) p[i] = p2[i] = i;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
join(u, v);
adj[u].push_back({i, v});
adj[v].push_back({i, u});
edges[i] = {u, v};
}
for (int i = 1; i <= n; i++) {
if (!dfn[i]) {
dfs(i, -1);
processBlock(-1);
}
}
set<int> currNodes;
for (int i = 0; i < m; i++) {
if (fucked[i]) continue;
int u = edges[i].first;
int v = edges[i].second;
currNodes.insert(u);
currNodes.insert(v);
g[u].push_back(v);
g[v].push_back(u);
join2(u, v);
}
memset(color, -1, sizeof color);
for (int u : currNodes)
if (color[u] == -1) dfsColor(u, 0);
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
int u, v;
scanf("%d %d", &u, &v);
if (get(u) != get(v)) {
printf("No\n");
continue;
}
if (get2(u) != get2(v)) {
printf("Yes\n");
continue;
}
if (color[u] == color[v])
printf("No\n");
else
printf("Yes\n");
}
}
int main() {
int t = 1;
for (int testCase = 1; testCase <= t; testCase++) {
solve(testCase);
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
struct graph {
struct point {
int next, to;
} ar[200050];
int head[200050], art = 1;
inline void link(int u, int v) {
ar[++art] = {head[u], v}, head[u] = art;
ar[++art] = {head[v], u}, head[v] = art;
}
inline void link1(int u, int v) { ar[++art] = {head[u], v}, head[u] = art; }
} G, G1, G2;
bool is[200050];
int dfn[200050], low[200050], tim, a2[200050], sta[200050], top, a[200050],
a1[200050], rt[200050], a3[200050], dep[200050], dp[200050][17], a4[200050];
inline bool dfs(int, int);
inline int lca(int, int);
inline void read(int&), tarjan(int), prepare(), Min(int&, int), dfs1(int),
dfs2(int);
int main() {
int n, m;
read(n), read(m), prepare();
for (int i(1), u, v; i <= m; ++i) read(u), read(v), G.link(u, v);
for (int i(1); i <= n; ++i)
if (!rt[i]) rt[i] = i, dfs1(i);
for (int i(1); i <= n; ++i)
if (!dfn[i]) top = 0, tarjan(i);
tim = 0;
for (int i(1); i <= n; ++i)
if (rt[i] == i) dfs2(i);
for (int i, j(1); j < 17; ++j)
for (i = 1; i + (1 << j) - 1 <= tim; ++i)
dp[i][j] = dep[dp[i][j - 1]] < dep[dp[i + (1 << j - 1)][j - 1]]
? dp[i][j - 1]
: dp[i + (1 << j - 1)][j - 1];
int Q;
read(Q);
for (int i(1), u, v; i <= Q; ++i)
if (read(u), read(v), rt[u] ^ rt[v])
puts("No");
else if (dep[u] & 1 ^ dep[v] & 1)
puts("Yes");
else
puts(a3[u] + a3[v] - (a3[lca(u, v)] << 1) ? "Yes" : "No");
return 0;
}
inline int lca(int x, int y) {
x = a[x], y = a[y];
if (x > y) x ^= y ^= x ^= y;
int len(a4[y - x + 1]);
return dep[dp[x][len]] < dep[dp[y - (1 << len) + 1][len]]
? dp[x][len]
: dp[y - (1 << len) + 1][len];
}
inline void prepare() {
for (int i(0); 1 << i < 200050; ++i) a4[1 << i] = i;
for (int i(1); i < 200050; ++i) a4[i] = a4[i] ? a4[i] : a4[i - 1];
}
inline void dfs2(int x) {
dp[a[x] = ++tim][0] = x;
for (int i(G2.head[x]), i1; i; i = G2.ar[i].next)
a3[i1 = G2.ar[i].to] += a3[dp[++tim][0] = x], dfs2(i1);
}
inline void dfs1(int x) {
for (int i(G.head[x]), i1; i; i = G.ar[i].next)
if (!rt[i1 = G.ar[i].to])
G2.link1(x, i1), dep[i1] = dep[x] + 1, a2[i] = a2[i ^ 1] = i1,
rt[i1] = rt[x], dfs1(i1);
}
inline bool dfs(int x, int c) {
a1[x] = c;
for (int i(G1.head[x]), i1; i; i = G1.ar[i].next)
if (!~a1[i1 = G1.ar[i].to] && dfs(i1, c ^ 1))
return true;
else if (a1[i1] == c)
return true;
return false;
}
inline void Min(int& x, int y) { x = x < y ? x : y; }
inline void tarjan(int x) {
dfn[x] = low[x] = ++tim, sta[++top] = x;
for (int i(G.head[x]), j, k, i1, i2; i; i = G.ar[i].next)
if (!dfn[i1 = G.ar[i].to]) {
tarjan(i1), Min(low[x], low[i1]);
if (dfn[x] <= low[i1]) {
int at(0);
G1.art = 1;
do G1.head[a[++at] = sta[top]] = 0, is[a[at]] = 1;
while (sta[top--] ^ i1);
G1.head[x] = 0, is[x] = 1;
for (j = 1; j <= at; ++j)
for (k = G.head[a[j]]; k; k = G.ar[k].next)
if (is[i2 = G.ar[k].to]) {
if (i2 == x)
G1.link(x, a[j]);
else if (i2 < a[j])
G1.link(i2, a[j]);
}
for (j = 1; j <= at; ++j) a1[a[j]] = -1;
a1[x] = -1;
if (dfs(a[1], 0))
for (j = 1; j <= at; ++j)
for (k = G.head[a[j]]; k; k = G.ar[k].next)
if (is[G.ar[k].to] && a2[k]) ++a3[a2[k]], a2[k] = a2[k ^ 1] = 0;
for (j = 1; j <= at; ++j) is[a[j]] = 0;
is[x] = 0;
}
} else
Min(low[x], dfn[i1]);
}
inline void read(int& x) {
x ^= x;
register char c;
while (c = getchar(), c < '0' || c > '9')
;
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int DIM = 100005;
vector<pair<int, int>> tre;
vector<int> gph[DIM];
int par[DIM][17], dep[DIM];
long long odd[DIM], ps1[DIM];
int n, m;
void dfs(int x, int p) {
for (int i = 1; i < 17; i++) par[x][i] = par[par[x][i - 1]][i - 1];
for (auto &i : gph[x])
if (i != p) {
dep[i] = dep[x] + 1;
par[i][0] = x;
dfs(i, x);
}
}
int lca(int x, int y) {
if (dep[x] > dep[y]) swap(x, y);
int dx = dep[y] - dep[x];
for (int i = 0; i <= 16; i++) {
if ((dx >> i) & 1) y = par[y][i];
}
for (int i = 16; i >= 0; i--) {
if (par[x][i] != par[y][i]) {
x = par[x][i];
y = par[y][i];
}
}
if (x != y) return par[x][0];
return x;
}
struct disjoint {
int pa[DIM];
void init(int n) {
for (int i = 1; i <= n; i++) {
pa[i] = i;
}
}
int find(int x) { return pa[x] = (pa[x] == x ? x : find(pa[x])); }
bool unite(int p, int q) {
p = find(p);
q = find(q);
if (p == q) return 0;
if (dep[p] > dep[q]) swap(p, q);
pa[q] = p;
return 1;
}
bool same_set(int p, int q) { return find(p) == find(q); }
} ds1, ds2, ds3;
void dfs2(int x, int p) {
ps1[x] += (ds3.find(x) != x && odd[ds3.find(x)]);
for (auto &i : gph[x]) {
if (i == p) continue;
ps1[i] += ps1[x];
dfs2(i, x);
}
}
int main() {
cin >> n >> m;
ds1.init(n);
ds3.init(n);
for (int i = 0; i < m; i++) {
int s, e;
cin >> s >> e;
if (ds1.unite(s, e)) {
gph[s].push_back(e);
gph[e].push_back(s);
} else
tre.emplace_back(s, e);
}
ds2 = ds1;
for (int i = 1; i <= n; i++) {
if (ds1.unite(1, i)) {
gph[1].push_back(i);
gph[i].push_back(1);
}
}
dfs(1, 0);
for (auto &i : tre) {
int l = lca(i.first, i.second);
for (int j = i.first;;) {
int u = ds3.find(j);
if (dep[u] > dep[l])
ds3.unite(u, par[u][0]);
else
break;
j = par[u][0];
}
for (int j = i.second;;) {
int u = ds3.find(j);
if (dep[u] > dep[l])
ds3.unite(u, par[u][0]);
else
break;
j = par[u][0];
}
if (dep[i.first] % 2 == dep[i.second] % 2) {
odd[l] = 1;
}
}
for (int i = 1; i <= n; i++) {
if (ds3.find(i) != i) {
odd[ds3.find(i)] |= odd[i];
odd[i] = 0;
}
}
dfs2(1, 0);
int q;
cin >> q;
while (q--) {
int x, y;
cin >> x >> y;
if (!ds2.same_set(x, y))
cout << "No\n";
else {
int l = lca(x, y);
int dst = dep[x] + dep[y] - dep[l] * 2;
long long sol = ps1[x] + ps1[y] - ps1[l] * 2ll;
cout << (dst % 2 == 0 && sol == 0 ? "No\n" : "Yes\n");
}
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 100010;
vector<int> G[N];
int n, m, Q, dep[N], fa[N], ffa[N];
int hson[N], top[N], sz[N], sum[N];
int dfn[N], dfc = 0;
bool vis[N], ans[N];
int qu[N], qv[N];
int idx[N];
int find(int x) { return ffa[x] == x ? x : ffa[x] = find(ffa[x]); }
void dfs1(int u) {
vis[u] = 1;
sz[u] = 1;
for (int v : G[u]) {
if (vis[v]) continue;
dep[v] = dep[u] + 1;
fa[v] = u;
dfs1(v);
sz[u] += sz[v];
if (sz[v] > sz[hson[u]]) hson[u] = v;
}
}
void dfs2(int u, int tp) {
dfn[u] = ++dfc;
top[u] = tp;
if (hson[u]) dfs2(hson[u], tp);
for (int v : G[u])
if (fa[v] == u && v != hson[u]) dfs2(v, v);
}
int LCA(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
x = fa[top[x]];
}
return dep[x] < dep[y] ? x : y;
}
bool query(int x, int y) {
if (find(x) != find(y)) return 0;
if ((dep[x] & 1) != (dep[y] & 1)) return 1;
return sum[x] + sum[y] - 2 * sum[LCA(x, y)];
}
void build(int u) {
vis[u] = 1;
for (int v : G[u]) {
if (fa[v] == u || fa[u] == v) continue;
if ((dep[u] & 1) != (dep[v] & 1)) continue;
sum[u]++;
sum[v]++;
sum[LCA(u, v)] -= 2;
}
for (int v : G[u])
if (!vis[v] && fa[v] == u) build(v);
}
void reduction1(int u) {
for (int v : G[u]) {
if (fa[v] != u) continue;
reduction1(v);
sum[u] += sum[v];
}
}
void reduction2(int u) {
for (int v : G[u]) {
if (fa[v] != u) continue;
sum[v] += sum[u];
reduction2(v);
}
}
void clear() {
memset(vis, 0, sizeof(vis));
memset(top, 0, sizeof(top));
memset(hson, 0, sizeof(hson));
memset(sz, 0, sizeof(sz));
memset(dep, 0, sizeof(dep));
memset(sum, 0, sizeof(sum));
memset(fa, 0, sizeof(fa));
dfc = 0;
memset(dfn, 0, sizeof(dfn));
}
int main() {
srand(time(0));
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) ffa[i] = i;
for (int i = 1, u, v; i <= m; i++) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
if (find(u) != find(v)) ffa[find(u)] = find(v);
}
scanf("%d", &Q);
for (int i = 1; i <= Q; i++) scanf("%d%d", qu + i, qv + i);
for (int i = 1; i <= n; i++) idx[i] = i;
for (int tms = 25; tms; tms--) {
random_shuffle(idx + 1, idx + 1 + n);
for (int i = 1; i <= n; i++) random_shuffle(G[i].begin(), G[i].end());
clear();
for (int i = 1; i <= n; i++)
if (!vis[idx[i]]) dfs1(idx[i]), dfs2(idx[i], idx[i]);
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++)
if (!vis[idx[i]]) {
build(idx[i]);
reduction1(idx[i]);
reduction2(idx[i]);
}
for (int i = 1; i <= Q; i++)
if (!ans[i]) ans[i] = query(qu[i], qv[i]);
}
for (int i = 1; i <= Q; i++) puts(ans[i] ? "Yes" : "No");
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <typename _Tp>
inline _Tp read(_Tp& x) {
char c11 = getchar(), ob = 0;
x = 0;
while (c11 ^ '-' && !isdigit(c11)) c11 = getchar();
if (c11 == '-') c11 = getchar(), ob = 1;
while (isdigit(c11)) x = x * 10 + c11 - '0', c11 = getchar();
if (ob) x = -x;
return x;
}
void file(void) {}
int n, m, val[200005], head[200005], cnt, f[200005], dfn[200005], low[200005],
idx, dep[200005], vis[200005], st[200005], top, g[200005], tot,
head2[200005], y, fa[200005][22];
struct edge {
int to, next, v;
} e[1000005];
void add(int a, int b) {
e[cnt].to = b;
e[cnt].next = head[a];
head[a] = cnt++;
}
void add2(int a, int b) {
e[cnt].to = b;
e[cnt].next = head2[a];
head2[a] = cnt++;
}
int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); }
void work(int u) {
int flag = 0;
for (int i = 1; i <= tot; i++) {
int x = e[g[i]].to;
for (int j = head2[x]; j; j = e[j].next)
if (u != x && dep[x] == dep[e[j].to]) flag = 1;
x = e[g[i] ^ 1].to;
for (int j = head2[x]; j; j = e[j].next)
if (u != x && dep[x] == dep[e[j].to]) flag = 1;
}
if (flag) {
for (int i = 1; i <= tot; i++) e[g[i]].v = e[g[i] ^ 1].v = 1;
}
}
void tarjan(int u, int pre) {
dfn[u] = low[u] = ++idx;
dep[u] = dep[pre] ^ 1;
for (int i = head[u]; i; i = e[i].next) {
if (e[i].to == pre) continue;
if (!dfn[e[i].to]) {
st[++top] = i;
tarjan(e[i].to, u);
low[u] = min(low[u], low[e[i].to]);
if (low[e[i].to] >= dfn[u]) {
y = 0, tot = 0;
while (y != i) {
y = st[top];
top--;
g[++tot] = y;
}
work(u);
}
} else {
low[u] = min(low[u], dfn[e[i].to]);
if (dfn[e[i].to] < dfn[u]) add2(u, e[i].to);
}
}
}
void prepare(int u) {
vis[u] = 1;
dep[u] = dep[fa[u][0]] + 1;
for (int i = 1; i <= 20; ++i) {
fa[u][i] = fa[fa[u][i - 1]][i - 1];
}
for (int i = head[u]; i; i = e[i].next) {
if (vis[e[i].to]) continue;
fa[e[i].to][0] = u;
val[e[i].to] = val[u] + e[i].v;
prepare(e[i].to);
}
}
int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = 20; i >= 0; --i) {
if (dep[fa[x][i]] >= dep[y]) x = fa[x][i];
}
if (x == y) return x;
for (int i = 20; i >= 0; --i) {
if (fa[x][i] != fa[y][i]) {
x = fa[x][i];
y = fa[y][i];
}
}
return fa[x][0];
}
int main() {
read(n);
idx = 0;
cnt = 2;
top = 0;
tot = 0;
read(m);
int a, b;
for (int i = 1; i <= n; ++i) f[i] = i;
for (int i = 1; i <= m; ++i) {
read(a);
read(b);
add(a, b);
add(b, a);
if (find(a) != find(b)) f[find(a)] = find(b);
}
for (int i = 1; i <= n; ++i)
if (!dfn[i]) tarjan(i, 0);
for (int i = 1; i <= n; ++i)
if (!vis[i]) prepare(i);
int q;
read(q);
while (q--) {
read(a);
read(b);
int lc = lca(a, b);
if (find(a) != find(b))
puts("No");
else if ((dep[a] & 1) != (dep[b] & 1))
puts("Yes");
else if (val[a] + val[b] - 2 * val[lc])
puts("Yes");
else
puts("No");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
char buf[1 << 20], *p1, *p2;
inline int _R() {
int o = 0;
char t =
(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2)
? 0
: *p1++);
while (!isdigit(t))
t = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2)
? 0
: *p1++);
while (isdigit(t))
o = o * 10 + t - 48,
t = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2)
? 0
: *p1++);
return o;
}
const int N = 1e5 + 5;
int X[N], Y[N], tot;
int dad[N], n, m;
int find(int x) { return x == dad[x] ? x : dad[x] = find(dad[x]); }
int La[N], To[N << 1], Ne[N << 1], TOT = 1;
int dfn[N], low[N], id[N], vst, S[N], top;
int dep[N], lab[N], bcc, blk;
int fa[20][N], *par = fa[0], go[20][N];
bool good[N];
vector<int> link[N];
void AE(int x, int y) {
Ne[++TOT] = La[x];
To[La[x] = TOT] = y;
}
void circle(int u) {
++bcc;
int v;
do {
v = S[top--];
lab[v] = bcc;
} while (v != u);
}
void tarjan(int u, int faE) {
int i, j, k, v;
dfn[u] = low[u] = ++vst;
id[u] = blk;
S[++top] = u;
for (i = La[u]; i; i = Ne[i]) {
if (i == faE) continue;
v = To[i];
if (!dfn[v]) {
par[v] = u;
link[u].push_back(v);
dep[v] = dep[u] + 1;
tarjan(v, i ^ 1);
low[u] = min(low[u], low[v]);
if (low[v] > dfn[u]) circle(v);
} else
low[u] = min(low[u], dfn[v]);
}
}
void dfs(int u) {
for (auto v : link[u]) {
go[0][v] = lab[u] == lab[v] && good[lab[u]];
for (int i = 1; i < 20; i++)
fa[i][v] = fa[i - 1][fa[i - 1][v]],
go[i][v] = go[i - 1][v] | go[i - 1][fa[i - 1][v]];
dfs(v);
}
}
bool Qry(int u, int v) {
if (id[u] != id[v]) return false;
if (dep[u] < dep[v]) swap(u, v);
int d = dep[u] - dep[v], i;
if (d & 1) return true;
for (i = 0; i < 20; i++)
if (d >> i & 1) {
if (go[i][u]) return true;
u = fa[i][u];
}
if (u == v) return false;
for (i = 19; ~i; i--)
if (fa[i][u] != fa[i][v]) {
if (go[i][u] || go[i][v]) return true;
u = fa[i][u], v = fa[i][v];
}
if (go[0][u] || go[0][v]) return true;
return false;
}
int rt[N], orz;
int main() {
int i, j, k, x, y;
n = _R();
m = _R();
for (i = 1; i <= n; i++) dad[i] = i;
for (i = 1; i <= m; i++) {
x = _R();
y = _R();
AE(x, y);
AE(y, x);
}
for (i = 1; i <= n; i++)
if (!dfn[i]) {
rt[++blk] = i;
tarjan(i, 0);
circle(i);
}
for (i = 1; i <= m; i++) {
x = To[i << 1];
y = To[i << 1 | 1];
if (lab[x] == lab[y] && (dep[x] & 1) == (dep[y] & 1)) good[lab[x]] = 1;
}
for (i = 1; i <= blk; i++) dfs(rt[i]);
m = _R();
while (m--) {
x = _R();
y = _R();
puts(Qry(x, y) ? "Yes" : "No");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000 + 5;
const int logs = 19;
int n, m, q, tot, temp, num, sum;
int head[MAXN], pre[MAXN][logs + 2], dep[MAXN], dfn[MAXN], low[MAXN], s[MAXN],
vis[MAXN], cir[MAXN], cx[MAXN], rt[MAXN], col[MAXN];
struct Edge {
int next, to;
} e[MAXN * 2];
inline int read() {
int x = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) x = x * 10 + c - 48, c = getchar();
return x;
}
inline void add_edge(int from, int to) {
e[++tot].next = head[from];
e[tot].to = to;
head[from] = tot;
}
void tarjan(int x, int fa) {
col[x] = sum;
pre[x][0] = fa;
s[++temp] = x;
dfn[x] = low[x] = ++num;
dep[x] = dep[fa] + 1;
vis[x] = 1;
for (int i = 1; i <= logs; i++) pre[x][i] = pre[pre[x][i - 1]][i - 1];
for (int i = head[x]; i; i = e[i].next) {
int v = e[i].to;
if (v == fa) continue;
if (!dfn[v]) {
tarjan(v, x);
low[x] = min(low[x], low[v]);
} else if (vis[v]) {
low[x] = min(low[x], dfn[v]);
if (dep[x] % 2 == dep[v] % 2) cir[x] = 1;
}
}
vis[x] = 0;
if (dfn[x] == low[x]) {
bool flag = false;
for (int i = temp; i >= 0; i--) {
if (s[i] == x) break;
if (cir[s[i]]) {
flag = true;
break;
}
}
if (flag) {
while (temp && s[temp] != x) {
cx[s[temp]]++;
vis[s[temp]] = 0;
--temp;
}
--temp;
} else {
while (temp && s[temp] != x) {
vis[s[temp]] = 0;
--temp;
}
--temp;
}
}
}
void dfs(int x) {
for (int i = head[x]; i; i = e[i].next) {
int v = e[i].to;
if (x == pre[v][0]) {
cx[v] += cx[x];
dfs(v);
}
}
}
inline int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = logs; i >= 0; i--)
if (dep[pre[x][i]] >= dep[y]) x = pre[x][i];
if (x == y) return x;
for (int i = logs; i >= 0; i--)
if (pre[x][i] != pre[y][i]) x = pre[x][i], y = pre[y][i];
return pre[x][0];
}
int main() {
n = read(), m = read();
int x, y;
for (int i = 1; i <= m; i++) {
x = read(), y = read();
add_edge(x, y);
add_edge(y, x);
}
for (int i = 1; i <= n; i++)
if (!col[i]) {
++sum;
rt[i] = 1;
tarjan(i, 0);
}
for (int i = 1; i <= n; i++)
if (rt[i]) dfs(i);
q = read();
for (int i = 1; i <= q; i++) {
x = read(), y = read();
if (col[x] == col[y]) {
int LCA = lca(x, y);
if ((dep[x] + dep[y] - dep[LCA] * 2) % 2 ||
(cx[x] + cx[y] - cx[LCA] * 2) > 0) {
printf("Yes\n");
continue;
}
}
printf("No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int f[100004], a[100004], P[100004], rt[100004];
int n, m, Q, dep[100004], par[17][100004];
vector<int> g[100004];
int F(int x) { return f[x] == x ? x : f[x] = F(f[x]); }
void dfs(int x, int depth) {
for (int i = 1; i < 17; i++)
if (par[i - 1][x] > 0) par[i][x] = par[i - 1][par[i - 1][x]];
dep[x] = depth;
for (int i = 0; i < g[x].size(); i++) {
int to = g[x][i];
if (!dep[to]) {
par[0][to] = x, dfs(to, depth + 1);
if (F(x) == F(to)) a[x] |= a[to];
} else if (dep[to] + 1 < dep[x]) {
if ((dep[x] - dep[to]) % 2 == 0) a[x] = 1;
for (int j = F(x); dep[j] > dep[to] + 1; j = F(j)) f[j] = par[0][j];
}
}
}
void dfs1(int x, int R) {
P[x] += a[x], rt[x] = R;
for (int i = 0; i < g[x].size(); i++) {
int to = g[x][i];
if (dep[to] == dep[x] + 1) {
if (F(to) == F(x)) a[to] |= a[x];
P[to] = P[x], dfs1(to, R);
}
}
}
int lca(int u, int v) {
if (dep[u] > dep[v]) swap(u, v);
for (int i = 16; i >= 0; i--)
if ((1 << i) & (dep[v] - dep[u])) v = par[i][v];
if (u == v) return u;
for (int i = 16; i >= 0; i--)
if (par[i][u] != par[i][v]) u = par[i][u], v = par[i][v];
return par[0][u];
}
bool ok(int a, int b) {
if (abs(dep[a] - dep[b]) % 2 == 1) return 1;
int L = lca(a, b);
if (P[a] + P[b] - 2 * P[L] > 0) return 1;
return 0;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) f[i] = i;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
g[u].push_back(v), g[v].push_back(u);
}
for (int i = 1; i <= n; i++)
if (!dep[i]) dfs(i, 1), dfs1(i, i);
scanf("%d", &Q);
while (Q--) {
int a, b;
scanf("%d%d", &a, &b);
if (rt[a] == rt[b] && ok(a, b))
puts("Yes");
else
puts("No");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 100100, L = 18;
vector<int> adj[N], cmp[N];
vector<pair<int, int> > vc, vc2;
map<int, bool> mp[N];
int par[N], p[N][L + 2], num[N], high[N], st[N], h[N], comp[N], c, start;
bool col[N], mark[N];
bool merge(int u, int v) {
bool f = (col[u] != col[v]);
int x = par[u], y = par[v];
if (x == y) return f;
if (cmp[x].size() < cmp[y].size()) swap(x, y);
for (int z : cmp[y]) {
cmp[x].push_back(z);
par[z] = x;
if (!f) col[z] = 1 - col[z];
}
cmp[y].clear();
return 1;
}
void DSU() {
for (auto u : vc2) {
for (int j = 0; j < 2; j++) {
if (!cmp[u.first].size()) cmp[u.first].push_back(u.first);
par[u.first] = u.first;
col[u.first] = 0;
swap(u.first, u.second);
}
}
bool flag(1);
for (auto u : vc2) flag &= merge(u.first, u.second);
for (auto u : vc2) {
if (!flag) mp[u.first][u.second] = 1;
cmp[u.first].clear(), cmp[u.second].clear();
}
vc2.clear();
return;
}
void CV(int v, int par) {
mark[v] = 1;
comp[v] = comp[par];
st[v] = high[v] = start++;
h[v] = h[par] + 1;
for (int u : adj[v]) {
if (!mark[u]) {
int sz = vc.size();
vc.push_back({v, u});
CV(u, v);
high[v] = min(high[v], high[u]);
if (high[u] >= st[v]) {
while (vc.size() != sz) {
vc2.push_back(vc.back());
vc.pop_back();
}
DSU();
}
} else if (u != par && st[u] < st[v]) {
high[v] = min(high[v], high[u]);
vc.push_back({v, u});
}
}
return;
}
void DFS(int v, int par) {
mark[v] = 1;
num[v] += num[par] + mp[par][v];
p[v][0] = par;
for (int i = 1; i < L; i++) p[v][i] = p[p[v][i - 1]][i - 1];
for (int u : adj[v])
if (!mark[u]) DFS(u, v);
return;
}
int LCA(int u, int v) {
if (h[u] < h[v]) swap(u, v);
int t = h[u] - h[v];
for (int i = 0; i < L; i++)
if (((1 << i) & t)) u = p[u][i];
if (u == v) return u;
for (int i = L - 1; i >= 0; i--)
if (p[v][i] != p[u][i]) v = p[v][i], u = p[u][i];
return p[v][0];
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m, q, u, v;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> u >> v;
adj[--u].push_back(--v);
adj[v].push_back(u);
}
for (int i = 0; i < n; i++)
if (!mark[i]) {
comp[i] = c++;
CV(i, i);
}
memset(mark, 0, sizeof mark);
for (int i = 0; i < n; i++)
if (!mark[i]) DFS(i, i);
cin >> q;
for (int i = 0; i < q; i++) {
cin >> u >> v;
u--, v--;
int anc = LCA(u, v);
if (comp[v] == comp[u] &&
(h[v] % 2 != h[u] % 2 || (num[v] + num[u] - 2 * num[anc]) > 0))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
struct E {
int ad, id, ne;
} e[100010 * 4];
int n, m, q, u[100010], v[100010], w[100010], h1[100010], h2[100010],
de[100010], fa[100010], f[100010], p0[100010], p1[100010];
bool o[100010];
void add(int* he, int x, int y, int d) {
static int t = 0;
++t, e[t].ne = he[x], he[x] = t, e[t].ad = y, e[t].id = d;
}
int ff(int* f, int x) {
if (f[x] == x)
return x;
else
return f[x] = ff(f, f[x]);
}
void ff1(int x) {
p0[x] = p1[x] = x;
for (int p = h1[x]; p; p = e[p].ne) {
int y = e[p].ad;
if (!de[y]) {
fa[y] = x, de[y] = de[x] + 1, ff1(y), p1[y] = x;
if (ff(p0, x) == ff(p0, y)) o[x] |= o[y];
} else if (de[y] + 1 < de[x]) {
int z = ff(p0, x);
while (de[z] > de[y] + 1) p0[z] = fa[z], z = ff(p0, z);
if ((de[x] - de[y]) % 2 == 0) o[x] = 1;
}
}
for (int p = h2[x]; p; p = e[p].ne)
if (de[e[p].ad]) w[e[p].id] = ff(p1, e[p].ad);
}
void ff2(int x) {
if (o[ff(p0, x)]) f[x]++;
for (int i = h1[x]; i; i = e[i].ne) {
int y = e[i].ad;
if (de[y] == de[x] + 1) {
if (ff(p0, x) == ff(p0, y)) o[y] |= o[x];
f[y] = f[x], ff2(y);
}
}
}
bool chk(int u, int v, int w) {
if (ff(p1, u) != ff(p1, v)) return 0;
if ((de[u] - de[v]) % 2) return 1;
return f[u] + f[v] - f[w] * 2 > 0;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1, x, y; i <= m; i++)
scanf("%d%d", &x, &y), add(h1, x, y, i), add(h1, y, x, i);
scanf("%d", &q);
for (int i = 1; i <= q; i++)
scanf("%d%d", u + i, v + i), add(h2, u[i], v[i], i), add(h2, v[i], u[i], i);
for (int i = 1; i <= n; i++)
if (!de[i]) de[i] = 1, ff1(i), ff2(i);
for (int i = 1; i <= q; i++) puts(chk(u[i], v[i], w[i]) ? "Yes" : "No");
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
int read() {
char k = getchar();
int x = 0;
while (k < '0' || k > '9') k = getchar();
while (k >= '0' && k <= '9') x = x * 10 + k - '0', k = getchar();
return x;
}
const int MX = 2e5 + 23;
int n, m;
int ORG[MX], CAC[MX], tot = 1;
struct edge {
int node, next, w;
} h[MX * 6];
void addedge(int u, int v, int *head, int flg = 1) {
h[++tot] = (edge){v, head[u], 1}, head[u] = tot;
if (flg) addedge(v, u, head, 0);
}
int __[MX];
void init() {
for (int i = 1; i < MX; ++i) __[i] = i;
}
int find(int x) { return __[x] == x ? x : __[x] = find(__[x]); }
void link(int u, int v) { __[find(u)] = find(v); }
int DFN[MX], low[MX], stk[MX], stktop, cnt, jishu[MX];
int pcnt, oddcyc, color[MX];
void tarjan(int x, int c = 0, int *head = ORG) {
DFN[x] = low[x] = ++cnt, stk[++stktop] = x;
color[x] = c;
for (int i = head[x], d; i; i = h[i].next) {
int odd = oddcyc;
if (h[i].w) {
if (color[d = h[i].node] == c) ++oddcyc;
h[i].w = h[i ^ 1].w = 0;
}
if (!DFN[d = h[i].node]) {
int QWQ = odd;
tarjan(d, c ^ 1);
odd = (oddcyc - odd) > 0;
low[x] = std::min(low[x], low[d]);
if (low[d] == DFN[x]) {
jishu[++pcnt] = odd;
color[pcnt] = 0;
for (int tmp = 0; tmp != d; --stktop) {
tmp = stk[stktop];
addedge(pcnt, tmp, CAC);
fprintf(stderr, "LINK %d %d\n", pcnt, tmp);
}
addedge(x, pcnt, CAC);
fprintf(stderr, "lINK %d %d\n", x, pcnt);
oddcyc = QWQ;
}
} else if (DFN[d] < low[x])
low[x] = DFN[d];
}
}
int hson[MX], size[MX], dep[MX], fa[MX];
int Sji[MX];
void dfs(int x, int *head = CAC) {
Sji[x] += jishu[x];
fprintf(stderr, "Sji[%d] = %d col = %d ,ji = %d\n", x, Sji[x], color[x],
jishu[x]);
size[x] = 1;
for (int i = head[x], d; i; i = h[i].next) {
if ((d = h[i].node) == fa[x]) continue;
fa[d] = x, dep[d] = dep[x] + 1;
Sji[d] += Sji[x];
dfs(d);
size[x] += size[d];
if (size[d] > size[hson[x]]) hson[x] = d;
}
}
int top[MX];
void dfs2(int x, int topf, int *head = CAC) {
top[x] = topf;
for (int i = head[x], d; i; i = h[i].next) {
if ((d = h[i].node) == fa[x]) continue;
dfs2(d, d == hson[x] ? topf : d);
}
}
int LCA(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) std::swap(x, y);
x = fa[top[x]];
}
return dep[x] < dep[y] ? x : y;
}
int query(int x, int y) {
if (x == y) return 0;
if (find(x) != find(y)) return 0;
int lca = LCA(x, y);
if (Sji[x] + Sji[y] - 2 * Sji[lca] + jishu[lca]) return 1;
if (color[x] ^ color[y]) return 1;
return 0;
}
int main() {
memset(color, -1, sizeof color);
init();
pcnt = n = read(), m = read();
for (int i = 1, u, v; i <= m; ++i) {
u = read(), v = read();
addedge(u, v, ORG);
link(u, v);
}
for (int i = 1; i <= n; ++i)
if (!DFN[i]) tarjan(i);
for (int i = 1; i <= pcnt; ++i)
fprintf(stderr, "color[%d] = %d\n", i, color[i]);
memset(DFN, 0, sizeof DFN);
for (int i = 1; i <= n; ++i) {
if (!DFN[find(i)]) {
DFN[find(i)] = 1;
dfs(find(i));
dfs2(find(i), find(i));
}
}
int q = read();
while (q--) {
int u = read(), v = read();
printf("%s\n", query(u, v) ? "Yes" : "No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline T1 max(T1 a, T2 b) {
return a < b ? b : a;
}
template <typename T1, typename T2>
inline T1 min(T1 a, T2 b) {
return a < b ? a : b;
}
const char lf = '\n';
namespace ae86 {
const int bufl = 1 << 15;
char buf[bufl], *s = buf, *t = buf;
inline int fetch() {
if (s == t) {
t = (s = buf) + fread(buf, 1, bufl, stdin);
if (s == t) return EOF;
}
return *s++;
}
inline int ty() {
int a = 0;
int b = 1, c = fetch();
while (!isdigit(c)) b ^= c == '-', c = fetch();
while (isdigit(c)) a = a * 10 + c - 48, c = fetch();
return b ? a : -a;
}
} // namespace ae86
using ae86::ty;
const int _ = 100007, __ = _ << 1, lglg = 18;
const char *YES = "Yes", *NO = "No";
int to[__], ne[__], he[_] = {0}, ecnt = 1;
inline void adde(int a, int b) {
to[++ecnt] = b, ne[ecnt] = he[a], he[a] = ecnt;
}
inline void addde(int a, int b) { adde(a, b), adde(b, a); }
int hav[__] = {0};
int bfa[_];
int findbfa(int x) { return bfa[x] == x ? x : bfa[x] = findbfa(bfa[x]); }
void linka(int a, int b) { bfa[findbfa(a)] = findbfa(b); }
int dep[_] = {0};
vector<int> e[_];
int ps[_], lps;
void make(int bas) {
int got = 0;
for (int i = 1; !got && i <= lps; i++) {
int x = to[ps[i]];
for (auto b : e[x])
if (x != bas && !((dep[x] ^ dep[b]) & 1)) {
got = 1;
break;
}
if (got) break;
x = to[ps[i] ^ 1];
for (auto b : e[x])
if (x != bas && !((dep[x] ^ dep[b]) & 1)) {
got = 1;
break;
}
}
if (!got) return;
for (int i = 1; i <= lps; i++) hav[ps[i]] = hav[ps[i] ^ 1] = 1;
}
int dfn[_] = {0}, low[_] = {0}, dcnt = 0, stk[_] = {0}, top = 0;
void dfs(int x, int ff) {
dfn[x] = low[x] = ++dcnt, dep[x] = dep[ff] + 1;
for (int i = he[x]; i; i = ne[i]) {
int b = to[i];
if (b == ff) continue;
if (!dfn[b]) {
stk[++top] = i, dfs(b, x);
low[x] = min(low[x], low[b]);
if (low[b] >= dfn[x]) {
lps = 0;
do {
ps[++lps] = stk[top--];
} while (stk[top + 1] != i);
make(x);
}
} else {
low[x] = min(low[x], dfn[b]);
if (dfn[b] < dfn[x]) e[x].emplace_back(b);
}
}
}
int ed[_] = {0}, fa[_] = {0}, pfa[_][lglg + 1] = {0}, sval[_] = {0};
void dfs2(int x, int ff) {
ed[x] = 1, fa[x] = ff;
for (int i = he[x]; i; i = ne[i]) {
int b = to[i];
if (b == ff || ed[b]) continue;
sval[b] = sval[x] + hav[i], dfs2(b, x);
}
}
int lca(int a, int b) {
if (dep[a] < dep[b]) swap(a, b);
for (int i = lglg; i >= 0; i--)
if (dep[pfa[a][i]] >= dep[b]) a = pfa[a][i];
if (a == b) return a;
for (int i = lglg; i >= 0; i--)
if (pfa[a][i] != pfa[b][i]) a = pfa[a][i], b = pfa[b][i];
if (a != b) a = fa[a], b = fa[b];
return a;
}
int n, m;
int main() {
ios::sync_with_stdio(0), cout.tie(nullptr);
n = ty(), m = ty();
for (int i = 1; i <= n; i++) bfa[i] = i;
for (int i = 1, a, b; i <= m; i++)
a = ty(), b = ty(), addde(a, b), linka(a, b);
for (int i = 1; i <= n; i++)
if (!dfn[i]) dfs(i, 0);
for (int i = 1; i <= n; i++)
if (!ed[i]) dfs2(i, 0);
for (int i = 1; i <= n; i++) pfa[i][0] = fa[i];
for (int i = 1; i <= lglg; i++)
for (int j = 1; j <= n; j++) pfa[j][i] = pfa[pfa[j][i - 1]][i - 1];
for (int qq = 1, qn = ty(); qq <= qn; qq++) {
int a = ty(), b = ty();
if (findbfa(a) != findbfa(b))
cout << NO << lf;
else if (((dep[a] ^ dep[b]) & 1))
cout << YES << lf;
else
cout << ((sval[a] + sval[b] - 2 * sval[lca(a, b)] > 0) ? YES : NO) << lf;
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAX = 200010;
vector<int> L[MAX];
struct data {
int a, b, ans, done;
} Q[MAX];
int ara[MAX];
namespace BCT {
vector<int> ed[MAX];
bool cut[MAX];
int tot, Time, low[MAX], st[MAX];
vector<int> bcc[MAX];
stack<int> S;
void popBCC(int s, int x) {
cut[s] = 1;
bcc[++tot].push_back(s);
while (bcc[tot].back() ^ x) {
bcc[tot].push_back(S.top());
S.pop();
}
}
void dfs(int s, int p = -1) {
S.push(s);
int ch = 0;
st[s] = low[s] = ++Time;
for (int x : ed[s]) {
if (!st[x]) {
ch++;
dfs(x, s);
low[s] = min(low[s], low[x]);
if (p != -1 && low[x] >= st[s])
popBCC(s, x);
else if (p == -1)
if (ch > 1) popBCC(s, x);
} else if (p != x)
low[s] = min(low[s], st[x]);
}
if (p == -1 && ch > 1) cut[s] = 1;
}
void processBCC(int n) {
for (int i = 1; i <= n; i++) bcc[i].clear();
memset(st, 0, sizeof(st));
memset(cut, 0, sizeof(cut));
Time = tot = 0;
for (int i = 1; i <= n; i++) {
if (!st[i]) {
dfs(i, -1);
if (!S.empty()) ++tot;
while (!S.empty()) {
bcc[tot].push_back(S.top());
S.pop();
}
}
}
}
int nn;
vector<int> tree[MAX];
int compNum[MAX];
bool oc;
int dis[MAX];
void ogo(int s, int p, int id) {
for (int x : BCT::ed[s]) {
if (compNum[x] != id) continue;
if (x == p or x == s) continue;
if (dis[x]) {
if (dis[x] % 2 == dis[s] % 2) oc = true;
} else {
dis[x] = dis[s] + 1;
ogo(x, s, id);
}
}
}
void buildTree(int n) {
processBCC(n);
for (int i = 1; i <= tot; i++) {
for (int v : bcc[i]) {
compNum[v] = i;
dis[v] = 0;
}
oc = false;
dis[bcc[i][0]] = 1;
ogo(bcc[i][0], bcc[i][0], i);
ara[i] = oc;
for (int q : bcc[i]) {
for (int r : L[q]) {
if (Q[r].done) continue;
int a = Q[r].a;
int b = Q[r].b;
if (compNum[a] == compNum[b]) {
Q[r].done = true;
if (ara[i])
Q[r].ans = true;
else if ((dis[a] % 2) != (dis[b] % 2)) {
Q[r].ans = true;
} else
Q[r].ans = false;
}
}
}
for (int v : bcc[i]) {
compNum[v] = i;
dis[v] = 0;
}
}
nn = tot;
for (int i = 1; i <= n; i++)
if (cut[i]) compNum[i] = ++nn;
for (int i = 1; i <= tot; i++) {
for (int v : bcc[i]) {
if (cut[v]) {
tree[i].push_back(compNum[v]);
tree[compNum[v]].push_back(i);
} else {
compNum[v] = i;
}
}
}
}
}; // namespace BCT
namespace HLD {
int ptr, par[MAX];
int sz[MAX], h[MAX], pos[MAX], head[MAX], base[MAX];
struct SegmentTree {
struct node {
int mn;
} tree[4 * MAX];
node Merge(node a, node b) {
node ret;
ret.mn = a.mn + b.mn;
return ret;
}
void build(int n, int st, int ed) {
if (st == ed) {
tree[n].mn = ara[base[st]];
return;
}
int mid = (st + ed) / 2;
build(2 * n, st, mid);
build(2 * n + 1, mid + 1, ed);
tree[n] = Merge(tree[2 * n], tree[2 * n + 1]);
}
node query(int n, int st, int ed, int i, int j) {
if (st >= i && ed <= j) return tree[n];
int mid = (st + ed) / 2;
if (mid < i)
return query(2 * n + 1, mid + 1, ed, i, j);
else if (mid >= j)
return query(2 * n, st, mid, i, j);
else
return Merge(query(2 * n, st, mid, i, j),
query(2 * n + 1, mid + 1, ed, i, j));
}
} st;
void dfs(int u, int far) {
sz[u] = 1, h[u] = far;
for (int v : BCT::tree[u])
BCT::tree[v].erase(find(BCT::tree[v].begin(), BCT::tree[v].end(), u));
for (int &v : BCT::tree[u]) {
par[v] = u;
dfs(v, far + 1);
sz[u] += sz[v];
if (sz[v] > sz[BCT::tree[u][0]]) swap(v, BCT::tree[u][0]);
}
}
void hld(int u) {
pos[u] = ++ptr;
base[ptr] = u;
for (int v : BCT::tree[u]) {
head[v] = (v == BCT::tree[u][0] ? head[u] : v);
hld(v);
}
}
inline int query(int n, int u, int v) {
int res = 0;
while (head[u] != head[v]) {
if (h[head[u]] > h[head[v]]) swap(u, v);
res += st.query(1, 1, n, pos[head[v]], pos[v]).mn;
v = par[head[v]];
}
if (h[u] > h[v]) swap(u, v);
res += st.query(1, 1, n, pos[u], pos[v]).mn;
return res;
}
void build(int n, int root, int x) {
ptr = 0;
for (int i = 1; i <= n; i++) {
if (!head[i]) {
head[i] = i;
h[i] = 0;
dfs(i, 0);
hld(i);
}
}
st.build(1, 1, n);
}
}; // namespace HLD
int D[MAX];
int mark[MAX];
int cur = 0;
void call(int s, int d) {
mark[s] = cur;
D[s] = d;
for (int x : BCT::ed[s]) {
if (mark[x] == 0) call(x, d + 1);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, q, a, b;
cin >> n >> m;
int x = -1;
for (int i = 1; i <= m; i++) {
cin >> a >> b;
if (x == -1) x = a;
BCT::ed[a].push_back(b);
BCT::ed[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
if (D[i] == 0) {
cur++;
call(i, 1);
}
}
cin >> q;
for (int i = 1; i <= q; i++) {
cin >> Q[i].a >> Q[i].b;
Q[i].done = Q[i].ans = 0;
if (Q[i].b == Q[i].a) {
Q[i].done = true;
Q[i].ans = false;
continue;
}
L[Q[i].a].push_back(i);
L[Q[i].b].push_back(i);
if (mark[Q[i].a] != mark[Q[i].b]) {
Q[i].done = true;
Q[i].ans = false;
}
}
BCT::buildTree(n);
HLD::build(BCT::nn, 1, n);
for (int i = 1; i <= q; i++) {
if (!Q[i].done) {
if (D[Q[i].a] % 2 != D[Q[i].b] % 2) {
Q[i].ans = 1;
} else {
int x = BCT::compNum[Q[i].a];
int y = BCT::compNum[Q[i].b];
Q[i].ans = HLD::query(BCT::nn, x, y);
}
}
if (Q[i].ans)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class T>
bool chkmin(T& a, T b) {
return a > b ? a = b, true : false;
}
template <class T>
bool chkmax(T& a, T b) {
return a < b ? a = b, true : false;
}
template <class T>
void read(T& a) {
char c = getchar();
a = 0;
T f = 1;
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) a = a * 10 + c - '0';
a *= f;
}
const int maxN = 1e5 + 5;
struct Edge {
int v, next;
} G[maxN << 1];
int st[maxN], e;
int n, m;
int dfn[maxN], low[maxN], dfs_clock, tp;
int g[maxN][20];
int cnt[maxN];
int deep[maxN];
pair<int, int> stk[maxN];
bool vis1[maxN], vis2[maxN];
int block[maxN], totblock;
void addedge(int u, int v) {
G[++e] = (Edge){v, st[u]};
st[u] = e;
}
void DFS(int u, int fr) {
vis1[u] = 1;
g[u][0] = fr;
deep[u] = deep[fr] + 1;
for (int i = (1), _ = (18); i <= _; ++i) g[u][i] = g[g[u][i - 1]][i - 1];
for (int e = st[u]; e; e = G[e].next) {
int v = G[e].v;
if (vis1[v]) continue;
DFS(v, u);
}
}
int LCA(int u, int v) {
if (deep[u] < deep[v]) swap(u, v);
for (int i = (18), _ = (0); i >= _; --i)
if (deep[g[u][i]] >= deep[v]) u = g[u][i];
if (u == v) return u;
for (int i = (18), _ = (0); i >= _; --i)
if (g[u][i] != g[v][i]) {
u = g[u][i];
v = g[v][i];
}
return g[u][0];
}
int getdis(int u, int v) { return deep[u] + deep[v] - 2 * deep[LCA(u, v)]; }
void tarjan(int u, int fr) {
low[u] = dfn[u] = ++dfs_clock;
for (int e = st[u]; e; e = G[e].next) {
int v = G[e].v;
if (v == fr) continue;
if (dfn[v] == 0) {
stk[++tp] = make_pair(u, v);
tarjan(v, u);
chkmin(low[u], low[v]);
if (low[v] >= dfn[u]) {
int o = tp;
bool flag = 0;
for (;;) {
int first = stk[tp].first, second = stk[tp].second;
if ((deep[first] & 1) == (deep[second] & 1)) {
flag = 1;
break;
}
tp--;
if (first == u && second == v) break;
}
tp = o;
for (;;) {
int first = stk[tp].first, second = stk[tp].second;
cnt[first] |= flag;
cnt[second] |= flag;
tp--;
assert(tp >= 0);
if (first == u && second == v) break;
}
cnt[u] = 0;
}
} else if (dfn[v] < dfn[u]) {
stk[++tp] = make_pair(u, v);
chkmin(low[u], dfn[v]);
}
}
}
void DFS2(int u, int fr) {
cnt[u] += cnt[fr];
vis2[u] = 1;
block[u] = totblock;
for (int e = st[u]; e; e = G[e].next) {
int v = G[e].v;
if (vis2[v]) continue;
DFS2(v, u);
}
}
int getsum(int u, int v) { return cnt[u] + cnt[v] - 2 * cnt[LCA(u, v)]; }
int main() {
read(n);
read(m);
for (int i = (1), _ = (m); i <= _; ++i) {
int u, v;
read(u);
read(v);
addedge(u, v);
addedge(v, u);
}
for (int i = (1), _ = (n); i <= _; ++i)
if (dfn[i] == 0) {
DFS(i, 0);
tarjan(i, 0);
++totblock;
DFS2(i, 0);
}
int q;
read(q);
for (int i = (1), _ = (q); i <= _; ++i) {
int u, v;
read(u);
read(v);
if (block[u] != block[v]) {
puts("No");
continue;
}
int ret = getsum(u, v);
puts((ret || (getdis(u, v) & 1)) ? "Yes" : "No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int f[1000005][20];
int dfn[1000005], low[1000005], zhan[1000005 * 2], head[1000005], bcc[1000005];
int pdltk[1000005], num, dep[1000005];
int n, m;
int vis1[1000005], vis2[1000005];
int cnt, tot;
int sum[1000005];
vector<int> e[1000005];
struct dian {
int x, y;
} s[2 * 1000005];
void dfs1(int u, int fa = 0) {
pdltk[u] = num;
dep[u] = dep[fa] + 1;
f[u][0] = fa;
vis1[u] = 1;
for (register int i = 1; i <= 19; i++) f[u][i] = f[f[u][i - 1]][i - 1];
for (register int i = 0; i < e[u].size(); i++) {
int v = e[u][i];
if (!vis1[v]) dfs1(v, u);
}
}
int top = 0;
void tarjan(int u, int fa = 0) {
static int idx = 0;
dfn[u] = low[u] = ++idx;
for (register int i = 0; i < e[u].size(); i++) {
int v = e[u][i];
if (v != fa && dfn[v] < dfn[u]) {
s[++top] = (dian){u, v};
if (!dfn[v]) {
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
int x, y, flag = 0, tmp = top;
do {
x = s[top].x;
y = s[top--].y;
if ((dep[x] & 1) == (dep[y] & 1)) {
flag = 1;
break;
}
} while (!(x == u && y == v));
if (!flag) continue;
top = tmp;
do {
x = s[top].x;
y = s[top--].y;
sum[x] = sum[y] = 1;
} while (!(x == u && y == v));
sum[u] = 0;
}
} else
low[u] = min(low[u], dfn[v]);
}
}
}
void dfs2(int u, int fa) {
sum[u] += sum[fa];
vis2[u] = 1;
for (register int i = 0; i < e[u].size(); i++)
if (!vis2[e[u][i]]) dfs2(e[u][i], u);
}
int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (register int i = 19; i >= 0; i--) {
if (dep[f[x][i]] >= dep[y]) x = f[x][i];
if (x == y) return x;
}
for (register int i = 19; i >= 0; i--) {
if (f[x][i] != f[y][i]) {
x = f[x][i];
y = f[y][i];
}
}
return f[x][0];
}
bool pd(int u, int v) {
if (u == v) return 0;
if (pdltk[u] != pdltk[v]) return 0;
if ((dep[u] & 1) ^ (dep[v] & 1)) return 1;
return (sum[u] + sum[v] - 2 * sum[lca(u, v)]) > 0;
}
int main() {
scanf("%d%d", &n, &m);
for (register int i = 1; i <= m; i++) {
int a1, a2;
scanf("%d%d", &a1, &a2);
e[a1].push_back(a2);
e[a2].push_back(a1);
}
for (register int i = 1; i <= n; i++)
if (!dfn[i]) {
num++;
dfs1(i);
tarjan(i);
dfs2(i, 0);
}
int t;
scanf("%d", &t);
for (register int i = 1; i <= t; i++) {
int u, v;
scanf("%d%d", &u, &v);
if (pd(u, v))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) x = x * 10 + c - 48, c = getchar();
return x;
}
inline int Min(int a, int b) { return a < b ? a : b; }
const int N = 1e5 + 5;
int head[N], to[N << 1], net[N << 1], cnt;
int dfn[N], low[N], num;
int sta[N], gs[N], tot;
int dep[N], co[N], f[N][20], l[N][20];
bool ins[N], jojo[N], rt[N];
int n, m, q, ln, ans, color;
inline void add(int x, int y) {
net[++cnt] = head[x];
to[cnt] = y;
head[x] = cnt;
net[++cnt] = head[y];
to[cnt] = x;
head[y] = cnt;
}
void tarjan(int now, int from) {
int i;
co[now] = color;
dfn[now] = low[now] = ++num;
dep[now] = dep[from] + 1;
f[now][0] = from;
sta[++tot] = now;
ins[now] = true;
for (int i = 1; i <= ln; ++i) f[now][i] = f[f[now][i - 1]][i - 1];
for (i = head[now]; i; i = net[i]) {
int nxt = to[i];
if (nxt == from) continue;
if (!dfn[nxt]) {
tarjan(nxt, now);
low[now] = Min(low[now], low[nxt]);
} else if (ins[nxt]) {
low[now] = min(dfn[nxt], low[now]);
if ((dep[now] & 1) == (dep[nxt] & 1)) jojo[now] = true;
}
}
ins[now] = false;
if (dfn[now] != low[now]) return;
bool pzk = false;
for (int i = tot; i >= 1; i--) {
if (sta[i] == now) break;
if (jojo[sta[i]]) {
pzk = true;
break;
}
}
if (pzk) {
while (tot && sta[tot] != now) {
gs[sta[tot]]++;
ins[sta[tot]] = false;
tot--;
}
tot--;
} else {
while (tot && sta[tot] != now) {
ins[sta[tot]] = false;
tot--;
}
tot--;
}
}
inline int LCA(int x, int y) {
int i;
if (dep[x] < dep[y]) swap(x, y);
for (i = ln; i >= 0; --i)
if (dep[f[x][i]] >= dep[y]) x = f[x][i];
if (x == y) return x;
for (i = ln; i >= 0; --i) {
if (f[x][i] != f[y][i]) {
x = f[x][i];
y = f[y][i];
}
}
return f[x][0];
}
void dfs(int now) {
for (int i = head[now]; i; i = net[i]) {
int t = to[i];
if (f[t][0] == now) {
gs[t] += gs[now];
dfs(t);
}
}
}
int main() {
n = read();
m = read();
int x, y;
for (int i = 1; i <= m; ++i) x = read(), y = read(), add(x, y);
q = read();
ln = log(n) / log(2);
for (int i = 1; i <= n; ++i) {
if (!dfn[i]) {
color++;
tarjan(i, 0);
rt[i] = true;
}
}
for (int i = 1; i <= n; ++i)
if (rt[i]) dfs(i);
int lca;
while (q--) {
x = read();
y = read();
if (co[x] != co[y])
puts("No");
else {
lca = LCA(x, y);
int len = dep[x] + dep[y] - 2 * dep[lca];
if (len & 1 || gs[x] + gs[y] - 2 * gs[lca])
puts("Yes");
else
puts("No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline int gi() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + ch - 48;
ch = getchar();
}
return x * f;
}
template <typename T>
inline bool Max(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
template <typename T>
inline bool Min(T &a, T b) {
return b < a ? a = b, 1 : 0;
}
const int N = 1e5 + 7;
int n, m, q, tp, len, edc = 1, tim;
int head[N], h2[N], St[N], tmp[N], vis[N], dep[N];
int fa[N][18], val[N], low[N], pre[N];
struct edge {
int last, to, w;
edge() {}
edge(int last, int to) : last(last), to(to) { w = 0; }
} e[N * 4];
void Add(int a, int b) {
e[++edc] = edge(head[a], b), head[a] = edc;
e[++edc] = edge(head[b], a), head[b] = edc;
}
void addedge(int a, int b) { e[++edc] = edge(h2[a], b), h2[a] = edc; }
void fuck(int rt) {
int fg = 0;
for (int j = 1; j <= len; ++j) {
int u = e[tmp[j]].to;
for (int i = h2[u]; i; i = e[i].last)
if (u != rt && dep[e[i].to] == dep[u]) fg = 1;
u = e[tmp[j] ^ 1].to;
for (int i = h2[u]; i; i = e[i].last)
if (u != rt && dep[e[i].to] == dep[u]) fg = 1;
}
if (fg)
for (int i = 1; i <= len; ++i) e[tmp[i]].w = e[tmp[i] ^ 1].w = 1;
}
void tarjan(int u, int fa) {
low[u] = pre[u] = ++tim;
for (int i = head[u], v = e[i].to; i; i = e[i].last, v = e[i].to)
if (v ^ fa) {
if (!low[v]) {
St[++tp] = i;
dep[v] = dep[u] ^ 1;
tarjan(v, u);
Min(pre[u], pre[v]);
if (pre[v] >= low[u]) {
len = 0;
for (int x = -1; x ^ i;) tmp[++len] = (x = St[tp--]);
fuck(u);
}
} else {
Min(pre[u], low[v]);
if (low[v] < low[u]) addedge(u, v);
}
}
}
void dfs(int u) {
vis[u] = tim;
for (int i = 1; i < 18; ++i) fa[u][i] = fa[fa[u][i - 1]][i - 1];
for (int i = head[u], v = e[i].to; i; i = e[i].last, v = e[i].to)
if (!vis[v]) {
fa[v][0] = u;
dep[v] = dep[u] + 1;
val[v] = val[u] + e[i].w;
dfs(v);
}
}
int Lca(int x, int y) {
if (dep[x] > dep[y]) swap(x, y);
for (int i = 17; ~i; --i)
if (dep[fa[y][i]] >= dep[x]) y = fa[y][i];
if (x == y) return x;
for (int i = 17; ~i; --i)
if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
int main() {
n = gi(), m = gi();
for (int i = 1; i <= m; ++i) Add(gi(), gi());
for (int i = 1; i <= n; ++i)
if (!low[i]) tarjan(i, 0);
tim = 0;
for (int i = 1; i <= n; ++i)
if (!vis[i]) dep[i] = 1, ++tim, dfs(i);
int Q = gi(), u, v;
while (Q--) {
u = gi(), v = gi();
if (vis[u] ^ vis[v])
puts("No");
else if (abs(dep[u] - dep[v]) % 2)
puts("Yes");
else if (val[u] + val[v] - 2 * val[Lca(u, v)])
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
return x * f;
}
const int MAXN = 300010;
const int INF = 2147483600;
int Node[MAXN << 1], Next[MAXN << 1], Root[MAXN << 1], cnt;
inline void insert(int u, int v) {
Node[++cnt] = v;
Next[cnt] = Root[u];
Root[u] = cnt;
return;
}
int N, M;
bool ji[MAXN + 1];
int pre[MAXN + 1][21];
int dfn[MAXN + 1], dep[MAXN + 1], tim, dist[MAXN + 1], top;
int sta[MAXN + 1], low[MAXN + 1], cir[MAXN + 1], col;
void DFS(int k) {
sta[++top] = k;
dfn[k] = low[k] = ++tim;
for (int i = 1; i < 20; i++) pre[k][i] = pre[pre[k][i - 1]][i - 1];
for (int x = Root[k]; x; x = Next[x]) {
int v = Node[x];
if (v == pre[k][0]) continue;
if (!dfn[v]) {
pre[v][0] = k, dep[v] = dep[k] + 1;
DFS(v);
low[k] = min(low[k], low[v]);
} else if (!cir[v]) {
low[k] = min(low[k], dfn[v]);
if ((dep[v] & 1) == (dep[k] & 1)) ji[k] = 1;
}
}
if (dfn[k] == low[k]) {
bool flag = 0;
int now = top;
++col;
while (sta[now] != k) flag |= ji[sta[now]], now--;
if (flag)
for (now++; now <= top; now++) dist[sta[now]]++;
now = sta[top];
cir[now] = col;
--top;
while (now != k) now = sta[top], top--, cir[now] = col;
}
return;
}
bool vis[MAXN + 1];
void dfs(int k) {
vis[k] = true;
for (int x = Root[k]; x; x = Next[x]) {
int v = Node[x];
if (v == pre[k][0] || vis[v]) continue;
dist[v] += dist[k], dfs(v);
}
return;
}
inline int LCA(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int i = 19; i >= 0; i--)
if (dep[u] - dep[v] >= (1 << i)) u = pre[u][i];
for (int i = 19; i >= 0; i--)
if (pre[u][i] != pre[v][i]) u = pre[u][i], v = pre[v][i];
if (u != v) return pre[u][0];
return u;
}
int main() {
N = read(), M = read();
for (int i = 1; i <= M; i++) {
int u = read(), v = read();
insert(u, v);
insert(v, u);
}
for (int i = 1; i <= N; i++)
if (!dfn[i]) dep[i] = 1, pre[i][0] = i, DFS(i);
for (int i = 1; i <= N; i++)
if (pre[i][0] == i) dfs(i);
int Q = read();
while (Q--) {
int u = read(), v = read();
if (pre[u][19] != pre[v][19])
puts("No");
else {
int x = LCA(u, v);
puts((((dep[u] + dep[v] - 2 * dep[x]) & 1) |
((dist[u] + dist[v] - 2 * dist[x]) > 0))
? "Yes"
: "No");
}
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
inline int gi() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * f;
}
int fir[100010], dis[400010], nxt[400010], id;
inline void link(int a, int b) { nxt[++id] = fir[a], fir[a] = id, dis[id] = b; }
int dep[100010], st[17][100010], tot[100010], rt[100010];
inline void dfs(int x) {
for (int i = fir[x]; i; i = nxt[i])
if (!dep[dis[i]])
st[0][dis[i]] = x, rt[dis[i]] = rt[x], dep[dis[i]] = dep[x] + 1,
dfs(dis[i]);
}
inline int LCA(int x, int y) {
if (dep[x] < dep[y]) std::swap(x, y);
for (int i = 16; ~i; --i)
if (dep[st[i][x]] >= dep[y]) x = st[i][x];
for (int i = 16; ~i; --i)
if (st[i][x] != st[i][y]) x = st[i][x], y = st[i][y];
if (x != y) x = st[0][x];
return x;
}
inline void dfs2(int x) {
for (int i = fir[x]; i; i = nxt[i]) {
if (st[0][dis[i]] != x) continue;
tot[dis[i]] += tot[x];
dfs2(dis[i]);
}
}
int dfn[100010], low[100010], stk[100010], tp, ins[100010], scc[100010],
yes[100010], totscc;
inline void tarjan(int x, int fa = -1) {
dfn[x] = low[x] = ++dfn[0];
stk[++tp] = x;
ins[x] = 1;
for (int i = fir[x]; i; i = nxt[i]) {
if (dis[i] == fa) continue;
if (!dfn[dis[i]])
tarjan(dis[i], x), low[x] = std::min(low[x], low[dis[i]]);
else
low[x] = std::min(low[x], dfn[dis[i]]);
}
if (dfn[x] == low[x]) {
++totscc;
while (stk[tp + 1] != x) ins[stk[tp]] = 0, scc[stk[tp]] = totscc, --tp;
}
}
int main() {
int n = gi(), m = gi(), a, b;
for (int i = 1; i <= m; ++i) a = gi(), b = gi(), link(a, b), link(b, a);
for (int i = 1; i <= n; ++i)
if (!dep[i]) dep[i] = 1, rt[i] = i, dfs(i);
for (int i = 1; i < 17; ++i)
for (int j = 1; j <= n; ++j) st[i][j] = st[i - 1][st[i - 1][j]];
for (int i = 1; i <= n; ++i)
if (dep[i] == 1) tarjan(i);
for (int x = 1; x <= n; ++x) {
for (int i = fir[x]; i; i = nxt[i]) {
if ((dep[x] + dep[dis[i]]) & 1) continue;
if (x >= dis[i]) continue;
if (scc[x] != scc[dis[i]]) continue;
yes[scc[x]] = 1;
}
}
for (int x = 1; x <= n; ++x)
if (st[0][x] && scc[x] == scc[st[0][x]] && yes[scc[x]]) ++tot[x];
for (int i = 1; i <= n; ++i)
if (dep[i] == 1) dfs2(i);
int Q = gi(), x, y, lca;
while (Q--) {
x = gi(), y = gi();
if (rt[x] != rt[y] || x == y) {
puts("No");
continue;
}
lca = LCA(x, y);
if (tot[x] + tot[y] - 2 * tot[lca] || (dep[x] + dep[y]) & 1)
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline void chkmax(T &a, T b) {
if (a < b) a = b;
}
template <class T>
inline void chkmin(T &a, T b) {
if (a > b) a = b;
}
inline int read() {
int sum = 0, p = 1;
char ch = getchar();
while (!(('0' <= ch && ch <= '9') || ch == '-')) ch = getchar();
if (ch == '-') p = -1, ch = getchar();
while ('0' <= ch && ch <= '9') sum = sum * 10 + ch - 48, ch = getchar();
return sum * p;
}
const int maxn = 1e5 + 20;
struct node {
int v, next;
};
node e[maxn << 1];
int cnt = 1, start[maxn], del[maxn << 1], odd[maxn << 1];
inline void addedge(int u, int v) {
e[++cnt] = (node){v, start[u]};
start[u] = cnt;
}
int n, m;
map<int, int> mp[maxn];
inline void init() {
n = read();
m = read();
for (int i = (1), _end_ = (m); i <= _end_; i++) {
int u = read(), v = read();
addedge(u, v);
addedge(v, u);
mp[u][v] = mp[v][u] = i;
}
}
int dfn[maxn], low[maxn], times, st[maxn], top, ste[maxn], tpe, vis[maxn];
vector<int> N[maxn];
vector<int> E[maxn];
vector<int> to[maxn];
int id[maxn], tot;
void dfs(int u, int fa) {
dfn[u] = low[u] = ++times;
st[++top] = u;
for (int i = start[(u)]; i; i = e[i].next) {
int v = e[i].v;
if (v == fa) continue;
if (!vis[i >> 1]) ste[++tpe] = i >> 1;
vis[i >> 1] = 1;
if (!dfn[v]) {
dfs(v, u);
chkmin(low[u], low[v]);
if (low[v] >= dfn[u]) {
int x;
++tot;
do {
x = st[top--];
N[tot].push_back(x);
} while (x != v);
N[tot].push_back(u);
do {
x = ste[tpe--];
E[tot].push_back(x);
} while (x != (i >> 1));
}
} else
chkmin(low[u], dfn[v]);
}
}
int fa[maxn];
int fin(int x) { return x == fa[x] ? x : fa[x] = fin(fa[x]); }
inline void merge(int x, int y) {
x = fin(x);
y = fin(y);
fa[y] = x;
}
int col;
int dis[maxn];
int flg;
void dfs1(int u) {
vis[u] = 1;
for (int v : to[u]) {
if (vis[v]) {
if (dis[u] ^ dis[v])
;
else {
flg = 1;
return;
}
continue;
}
dis[v] = dis[u] ^ 1;
dfs1(v);
}
}
int ff[maxn], p[maxn][20], cf[maxn], deep[maxn];
inline int lca(int u, int v) {
if (deep[u] < deep[v]) swap(u, v);
int dis = deep[u] - deep[v];
for (int i = (16), _end_ = (0); i >= _end_; i--)
if (dis >> i & 1) u = p[u][i];
if (u == v) return u;
for (int i = (16), _end_ = (0); i >= _end_; i--)
if (p[u][i] != p[v][i]) u = p[u][i], v = p[v][i];
return p[u][0];
}
void dfs2(int u, int fa) {
for (int i = start[(u)]; i; i = e[i].next)
if (del[i >> 1]) {
int v = e[i].v;
if (v == fa) continue;
cf[v] = cf[u] + odd[i >> 1];
p[v][0] = ff[v] = u;
deep[v] = deep[u] + 1;
dfs2(v, u);
}
}
inline void doing() {
for (int i = (1), _end_ = (n); i <= _end_; i++)
if (!dfn[i]) dfs(i, 0);
for (int i = (1), _end_ = (n); i <= _end_; i++) fa[i] = i;
for (int i = (1), _end_ = (m); i <= _end_; i++) {
int u = e[i << 1].v, v = e[i << 1 | 1].v;
if (fin(u) == fin(v)) continue;
merge(u, v);
del[i] = 1;
}
memset(vis, 0, sizeof(vis));
for (int i = (1), _end_ = (tot); i <= _end_; i++) {
col = i;
flg = 0;
for (int j : E[col])
to[e[j << 1].v].push_back(e[j << 1 | 1].v),
to[e[j << 1 | 1].v].push_back(e[j << 1].v);
dfs1(N[col].front());
for (int v : N[col]) vis[v] = 0, dis[v] = 0, to[v].clear();
for (int j : E[col]) odd[j] = flg;
}
for (int i = (1), _end_ = (n); i <= _end_; i++)
if (i == fin(i)) deep[i] = 1, dfs2(i, 0);
for (int j = (1), _end_ = (16); j <= _end_; j++)
for (int i = (1), _end_ = (n); i <= _end_; i++)
p[i][j] = p[p[i][j - 1]][j - 1];
int m = read();
for (int i = (1), _end_ = (m); i <= _end_; i++) {
int u = read(), v = read();
if (fin(u) != fin(v))
puts("No");
else {
int t = lca(u, v);
if (cf[u] + cf[v] - cf[t] * 2 || ((deep[u] + deep[v] - deep[t] * 2) & 1))
puts("Yes");
else
puts("No");
}
}
}
int main() {
init();
doing();
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x7fffffff, SINF = 0x3f3f3f3f;
const double PI = 3.141592653589793;
const long long LINF = 0x7fffffffffffffffLL, SLINF = 0x3f3f3f3f3f3f3f3fLL;
int _v, _f, _c;
long long _l;
void read() {
_c = getchar(), _v = _f = 0;
while (_c < '0' || _c > '9') {
if (_c == -1) {
_f = -1;
return;
} else if (_c == '-') {
_f = 1;
}
_c = getchar();
}
while (_c <= '9' && _c >= '0') {
_v = (_v << 3) + (_v << 1) + (_c ^ 48);
_c = getchar();
}
if (_f) {
_v = -_v;
}
}
void read_ll() {
_c = getchar(), _l = _f = 0;
while (_c < '0' || _c > '9') {
if (_c == -1) {
_f = -1;
return;
} else if (_c == '-') {
_f = 1;
}
_c = getchar();
}
while (_c <= '9' && _c >= '0') {
_l = (_l << 3) + (_l << 1) + (_c ^ 48);
_c = getchar();
}
if (_f) {
_l = -_l;
}
}
const int MAXN = 100005, MAXM = 100005, MAXK = 100005, MOD = 1e9 + 7;
const double EPS = 1e-8;
struct Edge {
int t, n;
} edge[MAXN * 2];
int head[MAXN], en;
void add_e(int f, int t) {
edge[en].t = t, edge[en].n = head[f], head[f] = en++;
}
int fa[MAXN], state[MAXN];
int get(int i) { return i == fa[i] ? i : (fa[i] = get(fa[i])); }
int ef[MAXN], et[MAXN], ec, num, eds, qus;
void init() {
for (int i = 0; i < num; i++) {
fa[i] = i, state[i] = false;
}
}
int dep[MAXN], p[MAXN][20];
void comb(int i, int j) {
i = get(i), j = get(j);
if (dep[i] < dep[j]) {
swap(i, j);
}
state[j] |= state[i];
fa[i] = j;
}
void dfs(int cur = 0, int last = -1) {
for (int i = head[cur]; ~i; i = edge[i].n) {
if (edge[i].t != last) {
p[edge[i].t][0] = cur;
for (int j = 1; j < 20; j++) {
p[edge[i].t][j] = p[p[edge[i].t][j - 1]][j - 1];
}
dep[edge[i].t] = dep[cur] + 1;
dfs(edge[i].t, cur);
}
}
}
int lca(int a, int b) {
if (dep[a] < dep[b]) {
swap(a, b);
}
for (int j = 19; ~j; j--) {
if (dep[p[a][j]] >= dep[b]) {
a = p[a][j];
}
}
if (a == b) {
return a;
}
for (int j = 19; ~j; j--) {
if (p[a][j] != p[b][j]) {
a = p[a][j], b = p[b][j];
}
}
return p[a][0];
}
void comb(int a, int b, int s) {
int l = lca(a, b), x = (a == l || b == l);
if (s) {
if (a == l) {
state[get(b)] = true;
} else {
state[get(a)] = true;
}
}
a = get(a), b = get(b);
while (dep[a] > dep[l] + 1 || dep[b] > dep[l] + 1) {
if (dep[a] < dep[b]) {
swap(a, b);
}
comb(a, p[a][0]);
a = get(a);
}
if (!x) {
comb(a, b);
}
}
int cnt[MAXN];
void dfs2(int cur = 0, int last = -1) {
if (!cur) {
cnt[cur] = state[get(cur)];
}
for (int i = head[cur]; ~i; i = edge[i].n) {
if (edge[i].t != last) {
cnt[edge[i].t] = cnt[cur] + state[get(edge[i].t)];
dfs2(edge[i].t, cur);
}
}
}
int main() {
memset(head, -1, sizeof(head));
num = (read(), _v) + 1, eds = (read(), _v);
init();
for (int i = 0; i < eds; i++) {
int f = (read(), _v), t = (read(), _v);
if (get(f) == get(t)) {
ef[ec] = f, et[ec] = t, ec++;
} else {
comb(f, t);
add_e(f, t), add_e(t, f);
}
}
for (int i = 1; i < num; i++) {
if (get(i) != get(0)) {
comb(i, 0);
add_e(i, 0), add_e(0, i);
}
}
init();
dfs();
for (int i = 0; i < ec; i++) {
comb(ef[i], et[i], (dep[ef[i]] ^ dep[et[i]] ^ 1) & 1);
}
dfs2();
qus = (read(), _v);
for (int i = 0; i < qus; i++) {
int f = (read(), _v), t = (read(), _v), l = lca(f, t);
if (!l) {
printf("No\n");
continue;
}
if ((dep[f] ^ dep[t]) & 1) {
printf("Yes\n");
} else {
int c = cnt[f] + cnt[t] - cnt[l] * 2;
printf(c ? "Yes\n" : "No\n");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
inline long long rd() {
long long x = 0, w = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * w;
}
struct graph {
int to[N << 1], nt[N << 1], hd[N], tot;
graph() { tot = 1; }
inline void add(int x, int y) {
++tot, to[tot] = y, nt[tot] = hd[x], hd[x] = tot;
++tot, to[tot] = x, nt[tot] = hd[y], hd[y] = tot;
}
} ee, tr;
int n, m, q;
bool p[N << 1];
int be[N], tot, dfn[N], low[N], ti, st[N << 1], tp, co[N];
int e[N], te, dd[N], td;
inline void tj(int x, int ffa) {
dfn[x] = low[x] = ++ti, be[x] = tot;
for (int i = ee.hd[x]; i; i = ee.nt[i]) {
int y = ee.to[i];
if (i == ffa || dfn[y] > dfn[x]) continue;
st[++tp] = i;
if (!dfn[y]) {
dd[++td] = i;
tr.add(ee.to[i], ee.to[i ^ 1]);
co[y] = co[x] ^ 1, tj(y, i ^ 1), low[x] = min(low[x], low[y]);
if (low[y] >= dfn[x]) {
int z = 0;
te = 0;
while (z != i) e[++te] = z = st[tp--];
bool ok = 0;
for (int j = 1; j <= te && !ok; ++j)
ok = co[ee.to[e[j]]] ^ co[ee.to[e[j] ^ 1]] ^ 1;
for (int j = 1; j <= te; ++j) p[e[j]] = p[e[j] ^ 1] = ok;
}
} else
low[x] = min(low[x], dfn[y]);
}
}
int fa[N], sz[N], hson[N], top[N], de[N], a[N];
void dfs1(int x) {
sz[x] = 1;
for (int i = tr.hd[x]; i; i = tr.nt[i]) {
int y = tr.to[i];
if (y == fa[x]) continue;
fa[y] = x, de[y] = de[x] + 1, a[y] = a[x] + p[dd[(i >> 1)]], dfs1(y),
sz[x] += sz[y];
hson[x] = sz[hson[x]] > sz[y] ? hson[x] : y;
}
}
void dfs2(int x, int ntp) {
top[x] = ntp;
if (hson[x]) dfs2(hson[x], ntp);
for (int i = tr.hd[x]; i; i = tr.nt[i]) {
int y = tr.to[i];
if (y == fa[x] || y == hson[x]) continue;
dfs2(y, y);
}
}
inline int glca(int x, int y) {
while (top[x] != top[y]) {
if (de[top[x]] < de[top[y]]) swap(x, y);
x = fa[top[x]];
}
return de[x] < de[y] ? x : y;
}
int main() {
n = rd(), m = rd();
for (int i = 1; i <= m; ++i) ee.add(rd(), rd());
for (int i = 1; i <= n; ++i)
if (!dfn[i]) tp = 0, ++tot, tj(i, 0), de[i] = 1, dfs1(i), dfs2(i, i);
q = rd();
while (q--) {
int x = rd(), y = rd();
if (be[x] ^ be[y])
puts("No");
else {
int lca = glca(x, y);
(((de[x] + de[y] - (de[lca] << 1)) & 1) |
(a[x] + a[y] - (a[lca] << 1) > 0))
? puts("Yes")
: puts("No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
return x * f;
}
const int N = 200020;
int dp[N][23], dep[N], fa[N];
int dfn[N], low[N], bel[N], tim, ret;
int st[N], top;
int odd[N];
int s[N];
int n, m;
vector<int> e[N];
inline void init_st(int u, int f) {
dep[u] = dep[f] + 1;
for (int i = 1; i <= 22; i++) {
dp[u][i] = dp[dp[u][i - 1]][i - 1];
}
for (int v : e[u]) {
if (!dep[v]) {
fa[v] = fa[u];
dp[v][0] = u;
init_st(v, u);
}
}
}
inline void tarjan(int u, int f) {
dfn[u] = low[u] = ++tim;
st[++top] = u;
for (int v : e[u]) {
if (v == f) continue;
if (!dfn[v]) {
tarjan(v, u);
low[u] = min(low[v], low[u]);
} else
low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u]) {
++ret;
int v;
do {
v = st[top--];
bel[v] = ret;
} while (u != v);
}
}
inline void check(int u) {
for (int v : e[u]) {
if (((dep[u] + dep[v]) % 2 == 0) && bel[u] == bel[v]) {
odd[bel[u]] = 1;
return;
}
}
}
inline void dfs(int u) {
for (int v : e[u]) {
if (dep[v] == dep[u] + 1) {
s[v] += s[u];
dfs(v);
}
}
}
inline int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
int delta = dep[x] - dep[y];
for (int i = 0; i <= 22; i++) {
if (delta & (1 << i)) {
x = dp[x][i];
}
}
if (x == y) return x;
for (int i = 22; i >= 0; i--) {
if (dp[x][i] != dp[y][i]) {
x = dp[x][i];
y = dp[y][i];
}
}
return dp[x][0];
}
inline int qwq(int u, int v) {
if (fa[u] != fa[v] || u == v ||
(!((dep[u] + dep[v]) % 2) && s[u] + s[v] == 2 * s[lca(u, v)]))
return 0;
return 1;
}
signed main() {
n = read(), m = read();
for (int i = 1; i <= m; i++) {
int u = read(), v = read();
e[u].push_back(v);
e[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
if (!dep[i]) {
fa[i] = i;
init_st(i, 0);
}
}
for (int i = 1; i <= n; i++) {
if (dep[i] == 1) {
tarjan(i, 0);
}
}
for (int u = 1; u <= n; u++) {
if (!odd[bel[u]]) {
check(u);
}
}
for (int i = 1; i <= n; i++) {
if (dp[i][0] && bel[i] == bel[dp[i][0]] && odd[bel[i]]) {
s[i]++;
}
}
for (int i = 1; i <= n; i++) {
if (dep[i] == 1) dfs(i);
}
int q = read();
for (int i = 1; i <= q; i++) {
int u = read(), v = read();
if (qwq(u, v))
puts("Yes");
else
puts("No");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
inline int gi() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * f;
}
int fir[100010], dis[400010], nxt[400010], id;
inline void link(int a, int b) { nxt[++id] = fir[a], fir[a] = id, dis[id] = b; }
int dep[100010], st[17][100010], tot[100010], rt[100010];
inline void dfs(int x) {
for (int i = fir[x]; i; i = nxt[i])
if (!dep[dis[i]])
st[0][dis[i]] = x, rt[dis[i]] = rt[x], dep[dis[i]] = dep[x] + 1,
dfs(dis[i]);
}
inline int LCA(int x, int y) {
if (dep[x] < dep[y]) std::swap(x, y);
for (int i = 16; ~i; --i)
if (dep[st[i][x]] >= dep[y]) x = st[i][x];
for (int i = 16; ~i; --i)
if (st[i][x] != st[i][y]) x = st[i][x], y = st[i][y];
if (x != y) x = st[0][x];
return x;
}
inline void dfs2(int x) {
for (int i = fir[x]; i; i = nxt[i]) {
if (st[0][dis[i]] != x) continue;
tot[dis[i]] += tot[x];
dfs2(dis[i]);
}
}
int dfn[100010], low[100010], stk[100010], tp, ins[100010], scc[100010],
yes[100010], totscc;
inline void tarjan(int x, int fa = -1) {
dfn[x] = low[x] = ++dfn[0];
stk[++tp] = x;
ins[x] = 1;
for (int i = fir[x]; i; i = nxt[i]) {
if (dis[i] == fa) continue;
if (!dfn[dis[i]])
tarjan(dis[i], x), low[x] = std::min(low[x], low[dis[i]]);
else
low[x] = std::min(low[x], dfn[dis[i]]);
}
if (dfn[x] == low[x]) {
++totscc;
while (stk[tp + 1] != x) ins[stk[tp]] = 0, scc[stk[tp]] = totscc, --tp;
}
}
int main() {
int n = gi(), m = gi(), a, b;
for (int i = 1; i <= m; ++i) a = gi(), b = gi(), link(a, b), link(b, a);
for (int i = 1; i <= n; ++i)
if (!dep[i]) dep[i] = 1, rt[i] = i, dfs(i);
for (int i = 1; i < 17; ++i)
for (int j = 1; j <= n; ++j) st[i][j] = st[i - 1][st[i - 1][j]];
for (int i = 1; i <= n; ++i)
if (dep[i] == 1) tarjan(i);
for (int x = 1; x <= n; ++x) {
for (int i = fir[x]; i; i = nxt[i]) {
if ((dep[x] + dep[dis[i]]) & 1) continue;
if (x >= dis[i]) continue;
if (scc[x] != scc[dis[i]]) continue;
yes[scc[x]] = 1;
}
}
for (int x = 1; x <= n; ++x)
if (st[0][x] && scc[x] == scc[st[0][x]] && yes[scc[x]]) ++tot[x];
for (int i = 1; i <= n; ++i)
if (dep[i] == 1) dfs2(i);
int Q = gi(), x, y, lca;
while (Q--) {
x = gi(), y = gi();
if (rt[x] != rt[y] || x == y) {
puts("No");
continue;
}
lca = LCA(x, y);
if (tot[x] + tot[y] - 2 * tot[lca] || (dep[x] + dep[y]) & 1)
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using std::vector;
const int maxn = 1e5 + 1;
struct Edge {
int u, v;
} e[maxn << 1];
int head[maxn], ecnt;
inline void addedge(int u, int v) {
e[++ecnt].v = v;
e[ecnt].u = head[u];
head[u] = ecnt;
}
inline void add(int u, int v) {
addedge(u, v);
addedge(v, u);
}
int fa[maxn], dep[maxn], top[maxn], son[maxn], siz[maxn], vis[maxn],
val[maxn << 2], idx[maxn], id, n, m, a, b, q, bcj[maxn], fx, fy;
inline int find(int x) { return bcj[x] == x ? x : bcj[x] = find(bcj[x]); }
inline void jh(int &x, int &y) { x ^= y ^= x ^= y; }
inline void pushdown(int rt, int l, int r) {
if (val[rt] != (r - l + 1)) return;
val[rt << 1] = (((l + r) >> 1) - l + 1);
val[rt << 1 | 1] = (r - ((l + r) >> 1));
}
inline void pushup(int rt) { val[rt] = val[rt << 1] + val[rt << 1 | 1]; }
inline void update(int rt, int l, int r, int L, int R) {
if (L <= l && r <= R) return (void)(val[rt] = r - l + 1);
pushdown(rt, l, r);
if (L <= ((l + r) >> 1)) update(rt << 1, l, ((l + r) >> 1), L, R);
if (R > ((l + r) >> 1)) update(rt << 1 | 1, ((l + r) >> 1) + 1, r, L, R);
pushup(rt);
}
inline int query(int rt, int l, int r, int L, int R) {
if (L <= l && r <= R) return val[rt];
pushdown(rt, l, r);
int ANS = 0;
if (L <= ((l + r) >> 1)) ANS += query(rt << 1, l, ((l + r) >> 1), L, R);
if (R > ((l + r) >> 1))
ANS += query(rt << 1 | 1, ((l + r) >> 1) + 1, r, L, R);
return ANS;
}
inline void upd(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) jh(x, y);
update(1, 1, n, idx[top[x]], idx[x]);
x = fa[top[x]];
}
if (dep[y] < dep[x]) jh(x, y);
update(1, 1, n, idx[x] + 1, idx[y]);
}
inline int qry(int x, int y) {
int cnt = 0;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) jh(x, y);
cnt += query(1, 1, n, idx[top[x]], idx[x]);
x = fa[top[x]];
}
if (dep[y] < dep[x]) jh(x, y);
cnt += query(1, 1, n, idx[x], idx[y]);
return cnt;
}
inline void dfs1(int x, int f) {
siz[x] = 1;
dep[x] = dep[f] + 1;
fa[x] = f;
for (int i = head[x], v; i; i = e[i].u) {
v = e[i].v;
if (dep[v]) continue;
dfs1(v, x);
siz[x] += siz[v];
if (siz[v] > siz[son[x]]) son[x] = v;
}
}
inline void dfs2(int x, int topf) {
idx[x] = ++id;
top[x] = topf;
if (!son[x]) return;
dfs2(son[x], topf);
for (int i = head[x], v; i; i = e[i].u) {
v = e[i].v;
if (dep[v] < dep[x] || v == son[x]) continue;
if (fa[v] == x) dfs2(v, v);
}
}
inline void dfs3(int x) {
for (int i = head[x], v; i; i = e[i].u) {
v = e[i].v;
if (v == fa[x]) continue;
if (fa[v] == x)
dfs3(v);
else if (dep[v] > dep[x])
continue;
else if (!((dep[x] - dep[v]) & 1))
upd(x, v);
}
}
inline void dfs4(int x) {
for (int i = head[x], v; i; i = e[i].u) {
v = e[i].v;
if (v == fa[x]) continue;
if (fa[v] == x)
dfs4(v);
else if (dep[v] > dep[x])
continue;
else if ((dep[x] - dep[v]) & 1) {
int cnt = qry(x, v) - query(1, 1, n, idx[v], idx[v]);
if (cnt > 0) upd(x, v);
}
}
}
inline int LCA(int x, int y) {
while (top[x] != top[y])
dep[top[x]] > dep[top[y]] ? x = fa[top[x]] : y = fa[top[y]];
return dep[x] < dep[y] ? x : y;
}
inline int dis(int x, int y) {
return (dep[x] + dep[y] - (dep[LCA(x, y)] << 1));
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) bcj[i] = i;
for (int i = 1; i <= m; i++) {
scanf("%d %d", &a, &b);
add(a, b);
fx = find(a);
fy = find(b);
bcj[fy] = fx;
}
for (int i = 1; i <= n; i++) {
if (dep[i]) continue;
dfs1(i, 0);
dfs2(i, i);
dfs3(i);
dfs4(i);
}
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
scanf("%d %d", &a, &b);
int lca = LCA(a, b);
if (find(a) != find(b))
puts("No");
else {
if (dis(a, b) & 1)
puts("Yes");
else if ((qry(a, b) - query(1, 1, n, idx[lca], idx[lca])) > 0)
puts("Yes");
else
puts("No");
}
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long Inf = 1e18;
const int N = 1e5 + 10;
const int mod = 0;
int gi() {
int x = 0, o = 1;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') o = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
template <typename T>
bool chkmax(T &a, T b) {
return a < b ? a = b, 1 : 0;
};
template <typename T>
bool chkmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
};
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }
void inc(int &a, int b) { a = (a + b >= mod ? a + b - mod : a + b); }
void dec(int &a, int b) { a = (a - b < 0 ? a - b + mod : a - b); }
int n, m, q, to[N << 1], nxt[N << 1], h[N],
tot = 1, dep[N], in[N], scc = 0, st[N], tp = 0, bel[N], dfn[N], low[N],
tim = 0, odd[N], col[N], anc[N][20], tag[N], fa[N];
int getf(int x) { return x == fa[x] ? x : fa[x] = getf(fa[x]); }
vector<int> E[N];
void adde(int u, int v) {
to[++tot] = v, nxt[tot] = h[u], h[u] = tot;
to[++tot] = u, nxt[tot] = h[v], h[v] = tot;
}
void tarjan(int u, int fa) {
anc[u][0] = fa;
for (int i = 1; i <= 19; i++) anc[u][i] = anc[anc[u][i - 1]][i - 1];
dfn[u] = low[u] = ++tim;
st[++tp] = u;
for (int i = h[u]; i; i = nxt[i]) {
int v = to[i];
if (v == fa) continue;
if (!dfn[v])
dep[v] = dep[u] + 1, in[i >> 1] = 1, tarjan(v, u),
low[u] = min(low[u], low[v]);
else
low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u]) {
int v;
++scc;
do v = st[tp--], bel[v] = scc;
while (u != v);
}
}
void color(int u, int c) {
col[u] = c;
for (int v : E[u]) {
if (!~col[v])
color(v, c ^ 1);
else if (col[u] == col[v])
odd[bel[u]] = 1;
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
int c = dep[u] - dep[v];
for (int i = 19; ~i; i--)
if (c >> i & 1) u = anc[u][i];
if (u == v) return u;
for (int i = 19; ~i; i--)
if (anc[u][i] != anc[v][i]) u = anc[u][i], v = anc[v][i];
return anc[u][0];
}
void dfs(int u, int fa) {
dfn[u] = 1;
if (fa) tag[u] += tag[fa];
for (int i = h[u]; i; i = nxt[i]) {
int v = to[i];
if (v == fa || !in[i >> 1]) continue;
dfs(v, u);
}
}
int main() {
n = gi(), m = gi();
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1, u, v; i <= m; i++)
u = gi(), v = gi(), adde(u, v), fa[getf(u)] = getf(v);
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i, 0);
for (int i = 2, x, y; i <= tot; i += 2)
if (bel[x = to[i]] == bel[y = to[i ^ 1]])
E[x].push_back(y), E[y].push_back(x);
memset(col, -1, sizeof(col));
for (int i = 1; i <= n; i++)
if (!~col[i]) color(i, 0);
for (int i = 2, x, y; i <= tot; i += 2)
if (in[i >> 1] && bel[x = to[i]] == bel[y = to[i ^ 1]] && odd[bel[x]]) {
if (dep[x] > dep[y]) swap(x, y);
++tag[y];
}
memset(dfn, 0, sizeof(dfn));
for (int i = 1; i <= n; i++)
if (!dfn[i]) dfs(i, 0);
q = gi();
while (q--) {
int u = gi(), v = gi();
if (getf(u) != getf(v))
puts("No");
else if ((dep[u] + dep[v]) & 1)
puts("Yes");
else if (tag[u] + tag[v] - 2 * tag[lca(u, v)])
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int n, m, q, curcol, col[100010], depth[100010], val[100010], sum[100010],
par[20][100010], parr[100010];
bool vis[100010];
vector<int> g[100010];
int FIND(int pos) {
if (parr[pos] != pos) parr[pos] = FIND(parr[pos]);
return parr[pos];
}
void dfs(int pos, int no, int d) {
vis[pos] = true;
col[pos] = curcol;
par[0][pos] = no;
depth[pos] = d;
for (int i = 0; i < g[pos].size(); i++) {
if (g[pos][i] == no) continue;
if (vis[g[pos][i]]) {
if (depth[pos] > depth[g[pos][i]] + 1) {
if ((depth[pos] - depth[g[pos][i]]) % 2 == 0) val[pos] = 1;
for (int j = FIND(pos); depth[j] > depth[g[pos][i]] + 1; j = FIND(j))
parr[j] = par[0][j];
}
continue;
}
dfs(g[pos][i], pos, d + 1);
if (FIND(pos) == FIND(g[pos][i])) val[pos] |= val[g[pos][i]];
}
}
void dfs2(int pos) {
sum[pos] += val[pos];
for (int i = 0; i < g[pos].size(); i++) {
if (depth[g[pos][i]] != depth[pos] + 1) continue;
if (FIND(pos) == FIND(g[pos][i])) val[g[pos][i]] |= val[pos];
sum[g[pos][i]] = sum[pos];
dfs2(g[pos][i]);
}
}
int lca(int pos, int pos2) {
if (depth[pos] < depth[pos2]) swap(pos, pos2);
for (int i = 18; i >= 0; i--)
if (depth[pos] - (int)pow(2, i) >= depth[pos2]) pos = par[i][pos];
for (int i = 18; i >= 0; i--) {
if (par[i][pos] != par[i][pos2]) {
pos = par[i][pos];
pos2 = par[i][pos2];
}
}
if (pos == pos2) return pos;
return par[0][pos];
}
int main() {
cin >> n >> m;
int x, y;
for (int i = 0; i < m; i++) {
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 1; i <= n; i++) parr[i] = i;
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
curcol = i;
dfs(i, 0, 0);
dfs2(i);
}
for (int i = 0; i < 18; i++) {
for (int j = 1; j <= n; j++) {
if (par[i][j] == 0) continue;
par[i + 1][j] = par[i][par[i][j]];
}
}
cin >> q;
for (int qn = 0; qn < q; qn++) {
scanf("%d%d", &x, &y);
if (col[x] != col[y]) {
puts("No");
continue;
}
int z = lca(x, y);
if ((depth[x] - depth[z] + depth[y] - depth[z]) % 2 == 1 ||
sum[x] + sum[y] - sum[z] * 2 > 0)
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.