Submission #44819127
Source Code Expand
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
#define Ed for(int i=h[x];~i;i=ne[i])
#define Ls(i,l,r) for(int i=l;i<r;++i)
#define Rs(i,l,r) for(int i=l;i>r;--i)
#define Le(i,l,r) for(int i=l;i<=r;++i)
#define Re(i,l,r) for(int i=l;i>=r;--i)
#define L(i,l) for(int i=0;i<l;++i)
#define E(i,l) for(int i=1;i<=l;++i)
#define W(t) while(t--)
#define Wh while
typedef long long ll;
#define add(a,b) (a+=b)>=mod&&(a-=mod)
#define sub(a,b) (a-=b)<0&&(a+=mod)
const int N=200010,mod=998244353;
int n,f[N],gans[N],ans[N],sz[N];
vector<int>g[N];
int find(int x){
if(f[x]==x)return x;
return f[x]=find(f[x]);
}
ll qpow(ll a,int b=mod-2){
ll res=1;
while(b){
if(b&1)res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
scanf("%d",&n);
E(i, n)f[i]=i,sz[i]=1,g[i].push_back(i);
E(i, n-1){
int a,b;
scanf("%d%d",&a,&b);
a=find(a),b=find(b);
if(sz[a]>sz[b])swap(a,b);
//merge a to b
ll inv=qpow(sz[a]+sz[b]);
int newv=sz[a]*inv%mod,newv2=sz[b]*inv%mod,old=newv2;
// printf("newv=%d/%d,newv2=%d/%d\n",sz[a],(sz[a]+sz[b]),sz[b],(sz[a]+sz[b]));
add(old,gans[b]);
for(int v:g[a]){
add(ans[v],gans[a]),add(ans[v],newv),sub(ans[v],old);
g[b].push_back(v);
}
gans[a]=0;
gans[b]=old;
newv2=old;
f[a]=b;
sz[b]+=sz[a];
sz[a]=0;
vector<int>().swap(g[a]);
}
E(j, n)
if(sz[j]){
E(i, n)add(ans[i],gans[j]);
break;
}
E(i, n)printf("%d ",ans[i]);
return 0;
}
Submission Info
| Submission Time |
|
| Task |
F - A Certain Game |
| User |
WUSICHENG |
| Language |
C++ 20 (gcc 12.2) |
| Score |
475 |
| Code Size |
1784 Byte |
| Status |
AC |
| Exec Time |
140 ms |
| Memory |
18228 KiB |
Compile Error
Main.cpp: In function ‘int main()’:
Main.cpp:38:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
38 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
Main.cpp:42:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
42 | scanf("%d%d",&a,&b);
| ~~~~~^~~~~~~~~~~~~~
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
475 / 475 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
example0.txt, example1.txt |
| All |
000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, example0.txt, example1.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 000.txt |
AC |
2 ms |
3176 KiB |
| 001.txt |
AC |
125 ms |
18172 KiB |
| 002.txt |
AC |
131 ms |
18172 KiB |
| 003.txt |
AC |
130 ms |
18116 KiB |
| 004.txt |
AC |
115 ms |
17848 KiB |
| 005.txt |
AC |
117 ms |
17840 KiB |
| 006.txt |
AC |
117 ms |
17840 KiB |
| 007.txt |
AC |
44 ms |
8556 KiB |
| 008.txt |
AC |
65 ms |
11084 KiB |
| 009.txt |
AC |
29 ms |
6980 KiB |
| 010.txt |
AC |
131 ms |
17408 KiB |
| 011.txt |
AC |
86 ms |
13764 KiB |
| 012.txt |
AC |
120 ms |
17308 KiB |
| 013.txt |
AC |
97 ms |
14620 KiB |
| 014.txt |
AC |
45 ms |
9012 KiB |
| 015.txt |
AC |
23 ms |
6324 KiB |
| 016.txt |
AC |
102 ms |
15280 KiB |
| 017.txt |
AC |
128 ms |
18228 KiB |
| 018.txt |
AC |
127 ms |
18168 KiB |
| 019.txt |
AC |
130 ms |
17804 KiB |
| 020.txt |
AC |
127 ms |
17896 KiB |
| 021.txt |
AC |
138 ms |
18172 KiB |
| 022.txt |
AC |
132 ms |
17760 KiB |
| 023.txt |
AC |
128 ms |
17896 KiB |
| 024.txt |
AC |
129 ms |
18000 KiB |
| 025.txt |
AC |
132 ms |
18016 KiB |
| 026.txt |
AC |
134 ms |
18072 KiB |
| 027.txt |
AC |
128 ms |
18148 KiB |
| 028.txt |
AC |
137 ms |
18088 KiB |
| 029.txt |
AC |
135 ms |
17852 KiB |
| 030.txt |
AC |
135 ms |
18160 KiB |
| 031.txt |
AC |
131 ms |
18020 KiB |
| 032.txt |
AC |
126 ms |
17992 KiB |
| 033.txt |
AC |
137 ms |
17932 KiB |
| 034.txt |
AC |
140 ms |
17852 KiB |
| 035.txt |
AC |
127 ms |
17904 KiB |
| 036.txt |
AC |
134 ms |
18080 KiB |
| 037.txt |
AC |
70 ms |
17288 KiB |
| 038.txt |
AC |
70 ms |
17256 KiB |
| example0.txt |
AC |
2 ms |
2972 KiB |
| example1.txt |
AC |
1 ms |
3268 KiB |