This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define mod 998244353
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int,int>
#define N 120005
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;/// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
struct upit{
int idx;
int x,y;
};
vector<upit> v;
int n,k;
vector<pii> graf[N];
map<pii,int> edge;
int br = 1;
int cale[N][20];
int good[N];
int suma[N][20];
int disc[N];
int out[N];
void dfs(int x, int y){
disc[x] = br;
cale[x][0] = y;
if(edge[{y,cale[y][0]}] > edge[{x,y}])good[x] = 1;
suma[x][0] = good[x];
ff(j,1,17){
cale[x][j] = cale[cale[x][j - 1]][j - 1];
suma[x][j] = suma[x][j - 1] + suma[cale[x][j - 1]][j - 1];
}
for(auto c:graf[x]){
if(c.xx == y)continue;
br++;
dfs(c.xx, x);
}
br++;
out[x] = br;
}
bool insubtree(int x, int y){
if(y == 0)return 1;
if(disc[x] >= disc[y] && disc[x] < out[y])return 1;
return 0;
}
int lca(int x, int y){
if(insubtree(y,x))return x;
fb(j,17,0)if(!insubtree(y,cale[x][j]))x = cale[x][j];
return cale[x][0];
}
int dist(int x, int y){
if(x == y)return 0;
int ans = 0;
fb(j,17,0){
if(!insubtree(cale[x][j],y)){
x = cale[x][j];
ans += (1<<j);
}
}
ans++;
return ans;
}
int koji(int x, int duz){
if(duz == 0)return x;
fb(j,17,0)if((1<<j)&duz)x = cale[x][j];
return x;
}
int sumado(int x, int duz){
int ans = 0;
fb(j,17,0){if((1<<j)&duz){x = cale[x][j]; ans += suma[x][j];}}
ans += suma[x][0];
return ans;
}
int dsu[N];
int sajz[N];
int findpar(int x){
if(x == dsu[x])return x;
return dsu[x] = findpar(dsu[x]);
}
void unite(int a, int b){
a = dsu[a];
b = dsu[b];
if(sajz[a] < sajz[b])swap(a,b);
dsu[a] = b;
sajz[b] += sajz[a];
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> k;
int dsd = 1;
ff(i,1,n + k - 1){
char t;
cin >> t;
int x,y;
cin >> x >> y;
if(t == 'S'){
graf[x].pb({y,dsd});
graf[y].pb({x,dsd});
edge[{x,y}] = edge[{y,x}] = dsd;
dsd++;
v.pb({0,x,y});
}
else v.pb({1,x,y});
}
dfs(1,0);
ff(i,1,n){dsu[i] = i; sajz[i] = 1;}
for(auto c:v){
if(c.idx == 0){
unite(c.x, c.y);
continue;
}
if(findpar(c.x) != findpar(c.y)){
cout << "no\n";
continue;
}
int brat = lca(c.x, c.y);
int l1 = dist(c.x, brat);
int l2 = dist(c.y, brat);
int e1 = -1e9;
int e2 = 1e9;
bool d1 = 1;
bool d2 = 1;
if(l1 >= 1)e1 = edge[{brat,koji(c.x,l1-1)}];
if(l2 >= 1)e2 = edge[{brat,koji(c.y,l2-1)}];
if(l1 >= 2){
if(sumado(c.x,l1-2) != 0)d1 = 0;
}
if(l2 >= 2){
if(sumado(c.x,l1-2) != l2-1)d2 = 0;
}
if(d1 == d2 && e1 < e2){
cout << "yes\n";
continue;
}
else cout << "no\n";
}
return 0;
}
Compilation message (stderr)
servers.cpp: In function 'void dfs(int, int)':
servers.cpp:6:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
servers.cpp:46:5: note: in expansion of macro 'ff'
46 | ff(j,1,17){
| ^~
servers.cpp: In function 'int lca(int, int)':
servers.cpp:7:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
7 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
| ^
servers.cpp:66:5: note: in expansion of macro 'fb'
66 | fb(j,17,0)if(!insubtree(y,cale[x][j]))x = cale[x][j];
| ^~
servers.cpp: In function 'int dist(int, int)':
servers.cpp:7:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
7 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
| ^
servers.cpp:72:5: note: in expansion of macro 'fb'
72 | fb(j,17,0){
| ^~
servers.cpp: In function 'int koji(int, int)':
servers.cpp:7:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
7 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
| ^
servers.cpp:83:5: note: in expansion of macro 'fb'
83 | fb(j,17,0)if((1<<j)&duz)x = cale[x][j];
| ^~
servers.cpp: In function 'int sumado(int, int)':
servers.cpp:7:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
7 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
| ^
servers.cpp:88:5: note: in expansion of macro 'fb'
88 | fb(j,17,0){if((1<<j)&duz){x = cale[x][j]; ans += suma[x][j];}}
| ^~
servers.cpp: In function 'int main()':
servers.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
servers.cpp:119:5: note: in expansion of macro 'ff'
119 | ff(i,1,n + k - 1){
| ^~
servers.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
servers.cpp:134:5: note: in expansion of macro 'ff'
134 | ff(i,1,n){dsu[i] = i; sajz[i] = 1;}
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |