Submission #677907

# Submission time Handle Problem Language Result Execution time Memory
677907 2023-01-04T15:20:43 Z Ahmed57 Usmjeri (COCI17_usmjeri) C++14
140 / 140
797 ms 70948 KB
#include <bits/stdc++.h>

using namespace std;
//DSU
int mn1 = 300001;
int pr1[300001],pr2[300001];
int gs1[300001],gs2[300001];
int findleader1(int x){
    if(pr1[x]==x){
        return x;
    }
    return pr1[x] = findleader1(pr1[x]);
}
bool samegroup1(int x,int y){
    int led1 = findleader1(x);
    int led2 = findleader1(y);
    return led1==led2;
}
void mergegroup1(int x,int y){
    int led1 = findleader1(x);
    int led2 = findleader1(y);
    if(led1==led2)return;
    if(gs1[led1]>gs1[led2]){
        pr1[led2]=led1;
        gs1[led1]+=gs1[led2];
    }else{
        pr1[led1]=led2;
        gs1[led2]+=gs1[led1];
    }
}
int findleader2(int x){
    if(pr2[x]==x){
        return x;
    }
    return pr2[x] = findleader2(pr2[x]);
}
bool samegroup2(int x,int y){
    int led1 = findleader2(x);
    int led2 = findleader2(y);
    return led1==led2;
}
void mergegroup2(int x,int y){
    int led1 = findleader2(x);
    int led2 = findleader2(y);
    if(led1==led2)return;
    if(gs2[led1]>gs2[led2]){
        pr2[led2]=led1;
        gs2[led1]+=gs2[led2];
    }else{
        pr2[led1]=led2;
        gs2[led2]+=gs2[led1];
    }
}
int dep[300001];
int P[300001][20];
int ed[300001];
int lca(int x,int y)
{
    if(dep[x]<dep[y]) swap(x,y);
    for(int k=19;k>=0;k--)
    {
        if(dep[x]-(1<<k) >= dep[y])
            x=P[x][k];
    }
    if(x==y) return x;
    for(int k=19;k>=0;k--)
    {
        if(P[x][k] != P[y][k])
            x=P[x][k],y=P[y][k];
    }
    return P[x][0];
}
vector<pair<int,int>> adj[300001];
void dfs(int i,int pr,int e){
    P[i][0] = pr;
    ed[i] = e;
    dep[i] = dep[pr]+1;
    for(int j = 1;j<20;j++){
        P[i][j] = P[P[i][j-1]][j-1];
    }
    for(auto j:adj[i]){
        if(j.first!=pr){
            dfs(j.first,i,j.second);
        }
    }
}
vector<int> e[300001];
int vis[300001] , col[300001];
bool ss = 1;
void color(int i,int c){
    vis[i] = 1;col[i] = c;
    for(auto j:e[i]){
        if(vis[j]==0){
            color(j,!c);
        }else{
            if(col[j]==c){
                ss = 0;
            }
        }
    }
}
int main(){
    //freopen("poklonin6.txt","r",stdin);
    for(int i = 0;i<mn1;i++){
        pr1[i] = i , pr2[i] = i , gs1[i] = 1, gs2[i] = 1;
    }
    int n,m;
    cin>>n>>m;
    for(int i = 0;i<n-1;i++){
        int a,b;cin>>a>>b;
        adj[a].push_back({b,i});
        adj[b].push_back({a,i});
    }
    dfs(1,0,0);
    vector<pair<int,int>> w;
    while(m--){
        int a,b;cin>>a>>b;
        int lc = lca(a,b);
        int cur = a , last = -1;
        if(dep[a]>dep[lc]){
            last = ed[a];
        }
        while(dep[cur]>dep[lc]){
            for(int j = 19;j>=0;j--){
                if(samegroup1(cur,P[cur][j])){
                    cur = P[cur][j];
                }
            }
            if(dep[cur]-1>dep[lc]){
                mergegroup1(cur,P[cur][0]);
            }
            if(dep[cur]>dep[lc]){
                mergegroup2(last,ed[cur]);
            }
            cur = P[cur][0];
            if(dep[cur]>dep[lc]){
                mergegroup2(last,ed[cur]);
            }
        }
        int last2 = last;
        swap(a,b);
        cur = a , last = -1;
        if(dep[a]>dep[lc]){
            last = ed[a];
        }
        while(dep[cur]>dep[lc]){
            for(int j = 19;j>=0;j--){
                if(samegroup1(cur,P[cur][j])){
                    cur = P[cur][j];
                }
            }
            if(dep[cur]-1>dep[lc]){
                mergegroup1(cur,P[cur][0]);
            }
            if(dep[cur]>dep[lc]){
                mergegroup2(last,ed[cur]);
            }
            cur = P[cur][0];
            if(dep[cur]>dep[lc]){
                mergegroup2(last,ed[cur]);
            }
        }
        if(last!=-1&&last2!=-1){
            w.push_back({last,last2});
        }
    }
    for(auto i:w){
        e[findleader2(i.first)].push_back(findleader2(i.second));
        e[findleader2(i.second)].push_back(findleader2(i.first));
    }
    set<int> s;
    long long ans = 1;
    for(int i = 0;i<n-1;i++){
        if(!vis[findleader2(i)]){
            color(findleader2(i),0);
            ans*=2;ans%=(1000000007);
        }
    }
    if(!ss){
        cout<<"0\n";
    }else cout<<ans;
}
# Verdict Execution time Memory Grader output
1 Correct 256 ms 36320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 512 ms 70948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 19236 KB Output is correct
2 Correct 12 ms 19296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19148 KB Output is correct
2 Correct 13 ms 19308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19796 KB Output is correct
2 Correct 15 ms 19796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19796 KB Output is correct
2 Correct 15 ms 19764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 625 ms 60576 KB Output is correct
2 Correct 689 ms 60368 KB Output is correct
3 Correct 353 ms 47168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 797 ms 64788 KB Output is correct
2 Correct 685 ms 64528 KB Output is correct
3 Correct 419 ms 49468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 694 ms 64784 KB Output is correct
2 Correct 605 ms 59776 KB Output is correct
3 Correct 492 ms 49092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 625 ms 65980 KB Output is correct
2 Correct 626 ms 65104 KB Output is correct
3 Correct 342 ms 45756 KB Output is correct