Submission #677903

# Submission time Handle Problem Language Result Execution time Memory
677903 2023-01-04T15:13:19 Z Ahmed57 Usmjeri (COCI17_usmjeri) C++14
70 / 140
614 ms 70476 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]>dep[lc]){
                mergegroup1(cur,P[cur][0]);
                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]>dep[lc]){
                mergegroup1(cur,P[cur][0]);
                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));
    }
    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 247 ms 36028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 464 ms 70476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 11 ms 19168 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 19136 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 16 ms 19668 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 16 ms 19752 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 614 ms 60068 KB Output is correct
2 Correct 594 ms 60124 KB Output is correct
3 Correct 300 ms 46992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 610 ms 64120 KB Output is correct
2 Correct 567 ms 64124 KB Output is correct
3 Incorrect 400 ms 49248 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 610 ms 64324 KB Output is correct
2 Correct 579 ms 59592 KB Output is correct
3 Correct 410 ms 48908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 605 ms 65352 KB Output is correct
2 Correct 610 ms 64852 KB Output is correct
3 Correct 335 ms 48916 KB Output is correct