QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#430395 | #4615. 榕树之心 | Naganohara_Yoimiya | 100 ✓ | 837ms | 19876kb | C++14 | 2.1kb | 2024-06-03 19:36:27 | 2024-06-03 19:36:28 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define mk make_pair
#define fi first
#define se second
using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
for(;(c<'0'||c>'9');c=getchar()){if(c=='-')f=-1;}
for(;(c>='0'&&c<='9');c=getchar())x=x*10+(c&15);
return x*f;
}
const int mod=998244353;
int ksm(int x,ll y,int p=mod){
int ans=1;y%=(p-1);
for(int i=y;i;i>>=1,x=1ll*x*x%p)if(i&1)ans=1ll*ans*x%p;
return ans%p;
}
int inv(int x,int p=mod){return ksm(x,p-2,p)%p;}
mt19937 rnd(time(0));
int randint(int l,int r){return rnd()%(r-l+1)+l;}
void add(int &x,int v){x+=v;if(x>=mod)x-=mod;}
void Mod(int &x){if(x>=mod)x-=mod;}
int cmod(int x){if(x>=mod)x-=mod;return x;}
template<typename T>void cmax(T &x,T v){x=max(x,v);}
template<typename T>void cmin(T &x,T v){x=min(x,v);}
const int N=1e5+5;
int f[N],sz[N],n,ID,dep[N];
vector<int>G[N];
void dp(int u,int fa){
sz[u]=1;
for(int v:G[u])if(v!=fa)dep[v]=dep[u]+1,dp(v,u),sz[u]+=sz[v];
int mx=0;
for(int v:G[u])if(v!=fa){
if(mx==0||f[mx]<f[v])mx=v;
}
if(!mx)return f[u]=0,void();
int lf=f[mx]+1;
for(int v:G[u])if(v!=fa&&v!=mx)lf-=sz[v];
if(lf<0)f[u]=(sz[u]%2==0?1:0);
else f[u]=lf;
}
set<pair<int,int> >S;
bool ans[N];
int sum=0;
void dfs(int u,int fa){
for(int v:G[u])if(v!=fa)sum+=sz[v],S.insert(mk(f[v]+1,v));
if(dep[u]%2==(n-1)%2){
auto [F,p]=*--S.end();
if(F>sum-sz[p])ans[u]=0;
else ans[u]=1;
}
for(int v:G[u])if(v!=fa){
S.erase(mk(f[v]+1,v)),sum-=sz[v];
dfs(v,u);
sum+=sz[v],S.insert(mk(f[v]+1,v));
}
for(int v:G[u])if(v!=fa)sum-=sz[v],S.erase(mk(f[v]+1,v));
}
void solve(){
n=read();
if(n==1){puts("1");return ;}
for(int i=2;i<=n;i++){
int u=read(),v=read();
G[u].emplace_back(v),G[v].emplace_back(u);
}
dp(1,0),dfs(1,0);
if(ID==3)cout<<ans[1]<<endl;
else{for(int i=1;i<=n;i++)cout<<ans[i];puts("");}
for(int i=1;i<=n;i++)G[i].clear(),f[i]=sz[i]=ans[i]=0;
S.clear();
}
signed main(void){
#ifndef ONLINE_JUDGE
freopen("5.in","r",stdin);
freopen("5.out","w",stdout);
#endif
ID=read();int tt=read();while(tt--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 20
Accepted
Test #1:
score: 20
Accepted
time: 0ms
memory: 7076kb
input:
1 50 15 1 2 2 3 3 4 2 5 1 6 1 7 7 8 2 9 7 10 4 11 10 12 8 13 5 14 1 15 15 1 2 1 3 1 4 3 5 5 6 6 7 4 8 4 9 2 10 10 11 6 12 10 13 3 14 5 15 15 1 2 2 3 2 4 1 5 2 6 1 7 7 8 8 9 8 10 7 11 4 12 2 13 7 14 1 15 15 1 2 1 3 3 4 3 5 3 6 3 7 6 8 5 9 3 10 10 11 7 12 2 13 8 14 8 15 15 1 2 1 3 2 4 2 5 4 6 1 7 6 8 ...
output:
101010011110000 100010111101010 101101010010110 100111100100011 100110010011000 101011100110000 101011010110101 100101100010111 101110000100111 101010101011100 001011000010110 100110100000101 101100110000010 101110011010101 101110010010100 101010100111010 000100010011000 000000101100011 001100010100...
result:
ok 50 lines
Subtask #2:
score: 20
Accepted
Test #2:
score: 20
Accepted
time: 565ms
memory: 18208kb
input:
2 20 100000 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52...
output:
010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101...
result:
ok 20 lines
Subtask #3:
score: 20
Accepted
Test #3:
score: 20
Accepted
time: 7ms
memory: 6020kb
input:
3 200 99 62 2 2 3 2 4 4 5 4 6 5 7 5 8 5 9 9 10 4 11 9 12 12 13 12 14 8 15 14 16 9 17 11 18 5 19 10 20 18 21 2 22 5 23 14 24 5 25 15 26 9 27 1 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52 52 53 53 54...
output:
0 0 1 0 1 1 1 0 1 1 0 0 1 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 1 0 0 1 1 1 1 0 0 1 1 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 0 0 1 1 0 0 0 0 1 0 0 1 1 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 1 0 0 1 0 0 0 1 1 1 0 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 ...
result:
ok 200 lines
Subtask #4:
score: 20
Accepted
Test #4:
score: 20
Accepted
time: 3ms
memory: 6224kb
input:
4 20 1000 1 2 2 3 1 4 1 5 2 6 6 7 6 8 6 9 2 10 10 11 5 12 4 13 5 14 5 15 13 16 1 17 2 18 16 19 6 20 13 21 21 22 19 23 11 24 12 25 21 26 7 27 1 28 23 29 3 30 9 31 19 32 1 33 24 34 34 35 35 36 36 37 30 38 22 39 1 40 26 41 34 42 40 43 14 44 6 45 31 46 20 47 16 48 4 49 23 50 7 51 40 52 5 53 29 54 25 55 ...
output:
010110111010000110011010100101011101001110011100000001011011111101110011010111001010011100101010011100001001100000101111100010011111001101110101010100011110110000010011000000001101100101000011100111110111100110101011001111110001111100010110101110110110100100000111100111101000000111010001101010011110...
result:
ok 20 lines
Subtask #5:
score: 20
Accepted
Dependency #2:
100%
Accepted
Dependency #4:
100%
Accepted
Test #5:
score: 20
Accepted
time: 837ms
memory: 19876kb
input:
5 20 100000 1 2 1 3 2 4 4 5 2 6 2 7 1 8 5 9 9 10 2 11 11 12 2 13 6 14 10 15 10 16 9 17 17 18 5 19 12 20 5 21 18 22 4 23 13 24 10 25 9 26 6 27 7 28 10 29 26 30 27 31 24 32 20 33 2 34 21 35 15 36 9 37 24 38 28 39 18 40 40 41 4 42 11 43 17 44 18 45 14 46 11 47 7 48 16 49 8 50 17 51 20 52 2 53 3 54 53 5...
output:
011010010101010010000111011100001011100101101011100100110101011110000011100111010010111000010001001010101100000101100010101001111100011001011111001100011001100111110100011111101011111101101011110011011111000010011000011110000100000110001101100101100101000100011100010100011001011100110110101100110101...
result:
ok 20 lines
Extra Test:
score: 0
Extra Test Passed