Submission #907624

# Submission time Handle Problem Language Result Execution time Memory
907624 2024-01-16T00:34:53 Z 8pete8 Usmjeri (COCI17_usmjeri) C++17
140 / 140
451 ms 162956 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 129 ms 74720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 248 ms 145236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 26204 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 26200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 26460 KB Output is correct
2 Correct 10 ms 26204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 26456 KB Output is correct
2 Correct 8 ms 26204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 323 ms 144408 KB Output is correct
2 Correct 362 ms 146296 KB Output is correct
3 Correct 202 ms 102992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 451 ms 162080 KB Output is correct
2 Correct 364 ms 161528 KB Output is correct
3 Correct 278 ms 117544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 360 ms 158548 KB Output is correct
2 Correct 357 ms 142584 KB Output is correct
3 Correct 307 ms 116560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 407 ms 162956 KB Output is correct
2 Correct 332 ms 159572 KB Output is correct
3 Correct 250 ms 98324 KB Output is correct