이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ordered_multiset tree<type1, null_type, less_equal<type1>, rb_tree_tag, tree_order_statistics_node_update>;
using namespace std;
using namespace __gnu_pbds;
const int mod = 1e9+7;
const double PI = acos(-1.0);
const double epsilon = 1e-6;
const int N = 1.2e5+5;
vector<int> g[N];
int n;
int sz[N], d[N], h[N], up[20][N], pos[N];
int top[N], chain[N], tin[N], tout[N], rt, timer;
struct SegmentTree{
int tree[N * 4];
int lazy[N * 4];
void build(const vector<int> &a,int v, int l, int r){
if(l == r){
tree[v] = a[l];
return;
}
int mid = (l + r) >> 1;
build(a,v * 2, l, mid);
build(a,v * 2 + 1, mid + 1, r);
tree[v] = tree[v * 2] + tree[v * 2 + 1];
}
void push(int v, int tl, int tr) {
if (lazy[v] == 0) return;
if (tl != tr) {
lazy[v * 2] += lazy[v];
lazy[v * 2 + 1] += lazy[v];
}
tree[v] += lazy[v];
lazy[v] = 0;
}
void update(int l, int r, int x, int v, int tl, int tr) {
push(v, tl, tr);
if (l > tr || tl > r) return ;
if (l <= tl && tr <= r) {
lazy[v] = x;
push(v, tl, tr);
return;
}
int tm = (tl + tr) / 2;
update(l, r, x, v * 2, tl, tm);
update(l, r, x, v * 2 + 1, tm + 1, tr);
tree[v] = tree[v * 2] + tree[v * 2 + 1];
}
int get(int v, int tl, int tr, int l, int r) {
push(v, tl, tr);
if (l > tr || tl > r) return 0;
if (l <= tl && tr <= r)return tree[v];
int tm = (tl + tr) / 2;
return get(v * 2, tl, tm, l, r) + get(v * 2 + 1, tm + 1, tr, l, r);
}
void clean(){
for(int i = 0; i < N * 4; i++) tree[i] = 0,lazy[i] = 0;
}
};
struct SegmentTreeMn{
int tree[N * 4];
int lazy[N * 4];
void build(const vector<int> &a,int v, int l, int r){
if(l == r){
tree[v] = a[l];
return;
}
int mid = (l + r) >> 1;
build(a,v * 2, l, mid);
build(a,v * 2 + 1, mid + 1, r);
tree[v] = min(tree[v * 2], tree[v * 2 + 1]);
}
void push(int v, int tl, int tr) {
if (lazy[v] == 0) return;
if (tl != tr) {
lazy[v * 2] += lazy[v];
lazy[v * 2 + 1] += lazy[v];
}
tree[v] += lazy[v];
lazy[v] = 0;
}
void update(int l, int r, int x, int v, int tl, int tr) {
push(v, tl, tr);
if (l > tr || tl > r) return ;
if (l <= tl && tr <= r) {
lazy[v] = x;
push(v, tl, tr);
return;
}
int tm = (tl + tr) / 2;
update(l, r, x, v * 2, tl, tm);
update(l, r, x, v * 2 + 1, tm + 1, tr);
tree[v] = min(tree[v * 2], tree[v * 2 + 1]);
}
int get(int v, int tl, int tr, int l, int r) {
push(v, tl, tr);
if (l > tr || tl > r) return 0;
if (l <= tl && tr <= r)return tree[v];
int tm = (tl + tr) / 2;
return min(get(v * 2, tl, tm, l, r), get(v * 2 + 1, tm + 1, tr, l, r));
}
void clean(){
for(int i = 0; i < N * 4; i++) tree[i] = 0,lazy[i] = 0;
}
int search(int v,int tl,int tr){
push(v,tl,tr);
if(tl == tr) return tl;
int tm = (tl + tr) / 2;
if(tree[v * 2] + lazy[v * 2] == tree[v]) return search(v * 2,tl,tm);
else return search(v * 2 + 1,tm + 1,tr);
}
};
SegmentTree seg,S;
SegmentTreeMn T;
void clean(){
T.clean();
S.clean();
seg.clean();
rt = 0; timer = 0;
d[1] = 0;
for(int i = 1; i < N; i++){
g[i].clear();
for(int j = 0; j < 20; j++) up[j][i] = 0;
top[i] = -1;
h[i] = -1;
pos[i] = 0;
}
}
void dfs(int u, int p){
up[0][u] = p;
for ( int i = 1; i < 20; i++ ){
up[i][u] = up[i - 1][up[i - 1][u]];
}
sz[u] = 1;
for ( auto &v: g[u] ){
if ( v != p ){
d[v] = d[u] + 1;
dfs(v, u);
sz[u] += sz[v];
}
}
for ( auto &v: g[u] ){
if ( v != p ){
if ( h[u] == -1 || sz[h[u]] < sz[v] ){
h[u] = v;
}
}
}
}
int lca(int u, int v){
if ( d[u] < d[v] ) swap(u, v);
int df = d[u] - d[v];
for ( int i = 0; i < 20; i++ ){
if ( df >> i & 1 ){
u = up[i][u];
}
}
if ( u == v ){
return u;
}
for ( int i = 19; i >= 0; i-- ){
if ( up[i][u] != up[i][v] ){
u = up[i][u];
v = up[i][v];
}
}
return up[0][u];
};
void HLD(int u, int p){
tin[u] = ++timer;
pos[timer] = u;
chain[u] = rt;
if ( top[rt] == -1 ){
top[rt] = u;
}
if ( h[u] != -1 ) HLD(h[u], u);
for ( auto &v: g[u] ){
if ( v != p && h[u] != v ){
rt++; HLD(v, u);
}
}
tout[u] = timer;
}
void upd(int u,int v,int val){
if ( d[u] < d[v] ) swap(u, v);
while ( chain[u] != chain[v] ){
int nxt = top[chain[u]];
seg.update(tin[nxt],tin[u],val,1,1,n);
T.update(tin[nxt],tin[u],val,1,1,n);
u = up[0][nxt];
}
seg.update(tin[v],tin[u],val,1,1,n);
T.update(tin[v],tin[u],val,1,1,n);
}
void update(int u,int v, int val){
int lc = lca(u,v);
upd(u,lc,val); upd(v,lc,val);
seg.update(tin[lc],tin[lc],-val,1,1,n);
T.update(tin[lc],tin[lc],-val,1,1,n);
}
int qry(int u,int v){
if ( d[u] < d[v] ) swap(u, v);
int res = 0;
while ( chain[u] != chain[v] ){
int nxt = top[chain[u]];
res += S.get(1,1,n,tin[nxt],tin[u]);
u = up[0][nxt];
}
res += S.get(1,1,n,tin[v],tin[u]);
return res;
}
int query(int u,int v){
int lc = lca(u,v);
int res = 0;
res += qry(u,lc);
res += qry(v,lc);
res -= S.get(1,1,n,tin[lc],tin[lc]);
return res;
}
void solve(){
cin >> n;
for(int i = 1; i < n; i++){
int u,v; cin >> u >> v;
g[u].pb(v); g[v].pb(u);
}
int m; cin >> m;
vector<pair<int,int> > qr(m);
for(auto &[l,r] : qr) cin >> l >> r;
dfs(1,1); HLD(1,1);
//
vector<int> IDK(n+1);
for(int i = 0; i < m; i++){
IDK[qr[i].ss] = i;
}
vector<int> A(n+1,1e9);
for(auto [u,v] : qr) A[tin[v]] = 0;
T.build(A,1,1,n);
vector<int> B(n+1,0);
for(auto [u,v] : qr) B[tin[u]] = 1;
S.build(B,1,1,n);
//
for(auto [u,v] : qr) update(u,v,1);
for(int time = 0; time < m; time++){
if(T.tree[1] != 1){
cout << "No\n";
return;
}
int ind = IDK[pos[T.search(1,1,n)]];
auto [u,v] = qr[ind];
update(u,v,-1); S.update(tin[u],tin[u],-1,1,1,n);
if(!(seg.get(1,1,N,tin[v],tin[v]) == 0 && query(u,v) == 0)){
cout << "No\n";
return;
}
T.update(tin[v],tin[v],1e9,1,1,n);
}
cout << "Yes\n";
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tt = 1;
cin >> tt;
while (tt--) {
clean();
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
jail.cpp:302:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
302 | main(){
| ^~~~
# | 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... |