This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
///*** Sown_Vipro ***///
/// ->TUYEN QUOC GIA<- ///
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("popcnt")
#define F first
#define S second
#define pb push_back
#define pi pair<int, int>
#define pii pair<int, pair<int, int> >
#define all(a) a.begin(), a.end()
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define REP(i, a, b) for(int i = a; i >= b; --i)
#define inp(name) if(fopen(name, "r")) freopen(name, "r", stdin);
#define out(name) if(fopen(name, "w")) freopen(name, "w", stdout);
#define szz(s) int(s.size())
const int N = 1e6 + 5, MAX = 1e6, oo = 1e9 + 5, MOD = 1e9 + 7;
int n, q, TIME, stt;
int s[N], p[N], h[N], nxt[N];
int chain[N], key[N], in[N], lz[4 * N], rs[4 * N];
pi st[4 * N];
pii Q[N];
vector<pi> e[N];
void pre_dfs(int u){
s[u] = 1;
for(auto [v, w] : e[u]){
if(v == p[u]) continue;
p[v] = u;
h[v] = h[u] + 1;
pre_dfs(v);
s[u] += s[v];
if(s[v] > s[nxt[u]]) nxt[u] = v;
}
}
void hld(int u, int p){
if(!key[stt]) key[stt] = u;
chain[u] = stt;
in[u] = ++TIME;
int c = nxt[u];
if(c) hld(c, u);
for(auto [v, w] : e[u]){
if(v != p && v != c){
++stt;
hld(v, u);
}
}
}
pi Merge(pi a, pi b){
return {min(a.F, b.F), max(a.S, b.S)};
}
void down(int id){
if(rs[id]){
st[2 * id].F = st[2 * id + 1].F = 0;
st[2 * id].S = st[2 * id + 1].S = 0;
lz[2 * id] = lz[2 * id + 1] = 0;
rs[2 * id] = rs[2 * id + 1] = 1;
rs[id] = 0;
}
if(lz[id]){
st[2 * id].F = st[2 * id + 1].F = lz[id];
st[2 * id].S = st[2 * id + 1].S = lz[id];
lz[2 * id] = lz[2 * id + 1] = lz[id];
lz[id] = 0;
}
}
void update(int id, int l, int r, int u, int v, int x){
if(l > v || r < u) return;
if(u <= l && r <= v){
st[id].F = st[id].S = x;
lz[id] = x;
return;
}
down(id);
int m = (l + r) / 2;
update(2 * id, l, m, u, v, x);
update(2 * id + 1, m + 1, r, u, v, x);
st[id] = Merge(st[2 * id], st[2 * id + 1]);
}
pi get(int id, int l, int r, int u, int v){
if(l > v || r < u) return {oo, -oo};
if(u <= l && r <= v){
return st[id];
}
down(id);
int m = (l + r) / 2;
return Merge(get(2 * id, l, m, u, v), get(2 * id + 1, m + 1, r, u, v));
}
void up(int u, int v, int x){
while(chain[u] != chain[v]){
if(chain[u] < chain[v]) swap(u, v);
update(1, 1, n, in[key[chain[u]]], in[u], x);
u = p[key[chain[u]]];
}
if(h[u] < h[v]) swap(u, v);
update(1, 1, n, in[v], in[u], x);
}
int check(int u, int v){
pi res = {oo, -oo};
while(chain[u] != chain[v]){
if(chain[u] < chain[v]) swap(u, v);
res = Merge(res, get(1, 1, n, in[key[chain[u]]], in[u]));
u = p[key[chain[u]]];
}
if(h[u] < h[v]) swap(u, v);
res = Merge(res, get(1, 1, n, in[v], in[u]));
return (res.F == res.S);
}
int dis(int u, int v){
int uu = u, vv = v;
while(chain[u] != chain[v]){
if(chain[u] < chain[v]) swap(u, v);
u = p[key[chain[u]]];
}
if(h[u] < h[v]) swap(u, v);
return (h[uu] + h[vv] - 2 * h[v]);
}
bool cmp(pii a, pii b){
return a.F > b.F;
}
void solve(){
cin >> n;
FOR(i, 2, n){
int u, v; cin >> u >> v;
e[u].pb({v, 0});
e[v].pb({u, 0});
}
pre_dfs(1);
hld(1, 0);
int t; cin >> t;
while(t--){
cin >> q;
FOR(i, 1, q){
cin >> Q[i].S.F >> Q[i].S.S;
Q[i].F = dis(Q[i].S.F, Q[i].S.S);
}
sort(Q + 1, Q + 1 + q, cmp);
int res = 1;
FOR(i, 1, q){
int u = Q[i].S.F, v = Q[i].S.S;
res &= check(u, v);
if(res == 0) break;
up(u, v, i);
}
// cout << res << "\n";
cout << (res ? "RO RANG\n" : "MAP MO\n");
st[1].F = st[1].S = lz[1] = 0;
rs[1] = 1;
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
inp("in.txt");
// out("ans.txt");
int t = 1;
cin >> t;
// while(t--){
solve();
// }
}
Compilation message (stderr)
diameter.cpp: In function 'int main()':
diameter.cpp:17:47: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | #define inp(name) if(fopen(name, "r")) freopen(name, "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~
diameter.cpp:175:5: note: in expansion of macro 'inp'
175 | inp("in.txt");
| ^~~
# | 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... |