Submission #907623

# Submission time Handle Problem Language Result Execution time Memory
907623 2024-01-16T00:34:39 Z 8pete8 Usmjeri (COCI17_usmjeri) C++17
140 / 140
425 ms 169392 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<float.h>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,ll>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
using namespace std;
#define int long long
#define double long double
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
const int mod=1e9+7,mxn=3e5,lg=30,inf=1e18,minf=-1e9,Mxn=100000;
int n,m;
vector<int>adj[mxn+10];
int up[mxn+10][lg+5],h[mxn+10],pa[mxn+10],val[mxn+10],sz[mxn+10];
int ans=1;
int find(int u){return ((pa[u]==u)?u:pa[u]=find(pa[u]));}
void get(int cur,int p){for(auto i:adj[cur])if(i!=p)h[i]=h[cur]+1,up[i][0]=cur,get(i,cur);}
void merg(int a,int b){
    a=find(a),b=find(b);
    if(a==b)return;
    if(h[a]>h[b])swap(a,b);
    pa[b]=a;
}
int lca(int a,int b){
    if(h[a]<h[b])swap(a,b);
    int k=h[a]-h[b];
    for(int i=0;i<=lg;i++)if(k&(1<<i))a=up[a][i];
    if(a==b)return a;
    for(int i=lg;i>=0;i--)if(up[a][i]!=up[b][i])a=up[a][i],b=up[b][i];
    return up[a][0];
}
vector<pii>bi[mxn+10];
void goup(int cur,int node){
    while(h[cur]>h[node]+1){
        bi[cur].pb({up[cur][0],0});
        bi[up[cur][0]].pb({cur,0});
        merg(cur,up[cur][0]);
        cur=find(cur);
    }
}
void checkbi(int cur,int col){
    if(val[cur]!=-1&&val[cur]!=col)return void(ans=0);
    val[cur]=col;
    for(auto &i:bi[cur]){
        if(val[i.f]!=-1&&val[i.f]!=val[cur]^i.s)ans=0;
        else if(val[i.f]==-1)checkbi(i.f,val[cur]^i.s);
    }
}
int32_t main(){
    fastio
    cin>>n>>m;
    vector<pii>check(m);
    for(int i=0;i<n-1;i++){
        int a,b;cin>>a>>b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    get(1,-1);
    for(int i=1;i<=n;i++)pa[i]=i;
    for(int i=0;i<m;i++)cin>>check[i].f>>check[i].s;
    for(int j=0;j<=lg;j++)up[1][j]=1;
    for(int j=1;j<=lg;j++)for(int i=2;i<=n;i++)up[i][j]=up[up[i][j-1]][j-1];
    for(auto i:check){
        int node=lca(i.f,i.s);
        if(i.f!=node)goup(i.f,node);
        if(i.s!=node)goup(i.s,node);
        if(i.f==node||i.s==node)continue;
        bi[i.f].pb({i.s,1});
        bi[i.s].pb({i.f,1});
    }
    fill(val,val+n+1,-1);
    for(int i=2;i<=n;i++)if(val[i]==-1)checkbi(i,1),ans=(ans*2)%mod;
    cout<<ans<<'\n';
}

Compilation message

usmjeri.cpp: In function 'void checkbi(long long int, long long int)':
usmjeri.cpp:71:34: warning: suggest parentheses around comparison in operand of '^' [-Wparentheses]
   71 |         if(val[i.f]!=-1&&val[i.f]!=val[cur]^i.s)ans=0;
      |                          ~~~~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 117 ms 74772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 253 ms 145152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 26200 KB Output is correct
2 Correct 8 ms 26204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 26204 KB Output is correct
2 Correct 8 ms 26100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 26460 KB Output is correct
2 Correct 9 ms 26312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 26456 KB Output is correct
2 Correct 9 ms 26460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 315 ms 144496 KB Output is correct
2 Correct 396 ms 154348 KB Output is correct
3 Correct 212 ms 106832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 425 ms 161872 KB Output is correct
2 Correct 377 ms 169392 KB Output is correct
3 Correct 285 ms 122960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 365 ms 158520 KB Output is correct
2 Correct 388 ms 150352 KB Output is correct
3 Correct 313 ms 121424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 412 ms 162900 KB Output is correct
2 Correct 341 ms 167508 KB Output is correct
3 Correct 258 ms 101972 KB Output is correct