QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#94786 | #6167. 染色图的联通性问题 | youngsystem | 100 ✓ | 1359ms | 139320kb | C++14 | 3.9kb | 2023-04-07 19:07:02 | 2023-04-07 19:07:05 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
int n=0,f=1,ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
n=n*10+ch-'0';
ch=getchar();
}
return n*f;
}
int c[1000005];
int zsl[1000005];
vector<int>v[1000005];
int tmp;
int sta[1000005],ttop;
int dfn[1000005],low[1000005],cnt;
void tarjan(int x,int f)
{
dfn[x]=++cnt;
low[x]=dfn[x];
sta[++ttop]=x;
bool flag=false;
for(int i=0;i<v[x].size();i++)
{
if(v[x][i]==f)continue;
if(dfn[v[x][i]]==0)
{
flag=true;
tarjan(v[x][i],x);
low[x]=min(low[x],low[v[x][i]]);
if(low[v[x][i]]>=dfn[x])
{
++tmp;
while(1)
{
v[tmp].push_back(sta[ttop]);
if(sta[ttop]==v[x][i])
{
ttop--;
break;
}
ttop--;
}
v[tmp].push_back(x);
}
}
else low[x]=min(low[x],dfn[v[x][i]]);
}
}
int zs[1000005],zsw[1000005];
int siz[1000005],son[1000005],fa[1000005];
void dfs1(int x,int f)
{
//printf("%d %d\n",x,f);
siz[x]=1;
fa[x]=f;
int maxn=-1;
for(int i=0;i<v[x].size();i++)
{
if(v[x][i]==f)continue;
dfs1(v[x][i],x);
siz[x]+=siz[v[x][i]];
if(siz[v[x][i]]>maxn)
{
maxn=siz[v[x][i]];
son[x]=v[x][i];
}
}
}
int nsl[1000005],ans,n;
void change(int x,int f,int opt)
{
if(x<=n)
{
//printf("???%d %d %d\n",x,c[x],nsl[c[x]]);
if(opt==1)
{
ans+=nsl[c[x]];
nsl[c[x]]++;
}
else if(opt==2)
{
nsl[c[x]]--;
ans-=nsl[c[x]];
}
}
for(int i=0;i<v[x].size();i++)
{
if(v[x][i]==f)continue;
change(v[x][i],x,opt);
}
}
bool vis[1000005];
void dfszs(int x,int f)
{
//printf("%d %d\n",x,f);
vis[x]=true;
for(int i=0;i<v[x].size();i++)
{
if(v[x][i]==f||v[x][i]==son[x])continue;
dfszs(v[x][i],x);
change(v[x][i],x,2);
}
if(son[x]!=0)dfszs(son[x],x);
for(int i=0;i<v[x].size();i++)
{
if(v[x][i]==f||v[x][i]==son[x])continue;
change(v[x][i],x,1);
}
if(x<=n)
{
ans+=nsl[c[x]];
nsl[c[x]]++;
}
zs[x]=ans;
}
void dfszsw(int x,int f)
{
vis[x]=true;
for(int i=0;i<v[x].size();i++)
{
if(v[x][i]==f||v[x][i]==son[x])continue;
dfszsw(v[x][i],x);
change(v[x][i],x,1);
}
if(son[x]!=0)dfszsw(son[x],x);
for(int i=0;i<v[x].size();i++)
{
if(v[x][i]==f||v[x][i]==son[x])continue;
change(v[x][i],x,2);
}
if(x<=n)
{
nsl[c[x]]--;
ans-=nsl[c[x]];
}
zsw[x]=ans;
}
int ff[1000005],zda[1000005];
int findf(int n)
{
if(ff[n]==n)return n;
return ff[n]=findf(ff[n]);
}
signed main()
{
int m,x,y;
n=read();
m=read();
for(int i=1;i<=n;i++)
{
c[i]=read();
zsl[c[i]]++;
}
int zans=0;
for(int i=1;i<=n;i++)
{
zans+=1LL*zsl[i]*(zsl[i]-1)/2;
}
int sl=0;
while(cin>>x>>y)
{
sl++;
if(sl>m)break;
v[x].push_back(y);
v[y].push_back(x);
}
tmp=n;
for(int i=1;i<=n;i++)if(dfn[i]==0)tarjan(i,0);
for(int i=1;i<=n;i++)v[i].clear();
for(int i=n+1;i<=tmp;i++)
{
for(int j=0;j<v[i].size();j++)
{
v[v[i][j]].push_back(i);
}
}
for(int i=1;i<=tmp;i++)ff[i]=i;
for(int i=1;i<=tmp;i++)
{
for(int j=0;j<v[i].size();j++)
{
//printf("%d %d\n",i,v[i][j]);
int tx=findf(i),ty=findf(v[i][j]);
if(tx==ty)continue;
ff[tx]=ty;
}
}
for(int i=1;i<=tmp;i++)
{
if(ff[i]!=i)continue;
dfs1(i,0);
}
//printf("orz1\n");
int het=0;
for(int i=1;i<=tmp;i++)
{
if(ff[i]!=i)continue;
dfszs(i,0);
het+=ans;
zda[i]=ans;
change(i,0,2);
}
//printf("orz2\n");
for(int i=1;i<=tmp;i++)
{
if(ff[i]!=i)continue;
change(i,0,1);
dfszsw(i,0);
}
//for(int i=1;i<=tmp;i++)printf("%lld %lld\n",zs[i],zsw[i]);
for(int i=1;i<=n;i++)
{
int qans=het-zda[findf(i)];
for(int j=0;j<v[i].size();j++)
{
if(v[i][j]==fa[i])continue;
qans+=zs[v[i][j]];
}
qans+=zsw[i];
printf("%lld\n",zans-qans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 10
Accepted
time: 5ms
memory: 48672kb
input:
114 4191 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 59 46 6 103 2 80 5 80 1 34 1 15 8 34 4 100 30 104 8 44 8 76 21 ...
output:
3321 2839 2922 3661 2839 3393 2922 3004 2755 2755 2670 3234 2839 3082 2839 2755 2922 2755 2755 2755 2839 2755 2755 2839 2839 2755 2839 2673 2755 3003 2755 2670 2670 2755 2755 2755 2755 2755 2670 2755 2755 2755 2755 2755 2755 2755 2755 2670 2755 2670 2755 2755 2670 2670 2670 2670 2670 2755 2839 2839 ...
result:
ok 114 numbers
Test #2:
score: 10
Accepted
time: 3ms
memory: 46628kb
input:
1000 138966 110 156 136 6 16 313 383 173 95 189 119 143 202 152 18 277 83 6 196 516 65 131 5 6 70 30 97 192 581 12 1 46 58 136 638 62 9 283 15 63 9 37 199 35 7 154 111 17 230 341 150 261 191 2 104 14 138 265 1 18 105 47 25 2 49 210 435 91 372 375 42 5 61 136 1 89 217 205 37 97 96 310 2 114 292 132 6...
output:
1307 1306 1310 1319 1313 1306 1306 1307 1308 1312 1308 1306 1306 1306 1306 1306 1309 1321 1328 1306 1307 1307 1318 1306 1306 1310 1306 1306 1306 1314 1326 1330 1312 1308 1306 1306 1306 1307 1310 1318 1313 1335 1306 1315 1313 1307 1307 1314 1307 1306 1306 1306 1307 1306 1326 1306 1306 1306 1306 1313 ...
result:
ok 1000 numbers
Test #3:
score: 10
Accepted
time: 908ms
memory: 120272kb
input:
485803 491610 4 2 5 1 1 1 1 2 1 1 1 1 1 93 9 6 5 2 1 1 1 1 1 2 1 1 1 1 1 1 29 1 1 11 3 1 38 1 37 2 4 1 4 1 88 5 1 1 1 3 1 1 16 239 1 1 3 58 6 2 4 10 99 4 1 3 36 2 1 12 6 10 1 1 1 11 1 245 1 1 1 1 3 26 9 1 1 1 4 18 3 21 199 2 1 1 15 1 3 73 2 1 1 3 1 1123 1 2 1 2 79 12 18 2 1 1 82 2 7 1 123 1 67 1 5 1...
output:
20194694547 19493997007 19313748429 19227113540 19183454881 19137103370 19115112249 19097340334 19085229697 19060305970 19051651040 19044836139 19033261691 19025698226 19015837661 19021910149 19010899898 19003777623 19012438007 18996480084 18996896171 18991515847 18987148855 18988872801 18987176032 ...
result:
ok 485803 numbers
Test #4:
score: 10
Accepted
time: 861ms
memory: 125540kb
input:
497602 455793 110 42 3 46 144 1 5 26 43 17 7 1 2 1 90 1 5 47 1 2 24 3 3 2 1 2 42 1 1 10 1 4 3 116 1 3 1 12 8 1 1 1 1 44 65 1 3 33 1 3 1 70 3 2 110 1 41 1 14 6 1 1 1 1 2 654 5 44 1 10 1 1 51 1 1 1 1 1 118 4 1 692 37 8 47 6074 153 18 179 20 7 1 8 70 5 86 4 12 1 1 1 14 1 15 1 1 3 5 12 1 3 21 261 39 9 1...
output:
10904417211 8292771032 7984597550 7861776264 7804878662 7769060556 7743403983 7728775469 7716551095 7706072924 7698506534 7690231431 7686033401 7681227786 7677014101 7675085295 7673208337 7667198692 7668760494 7668677247 7668250976 7661594535 7661507280 7659525900 7660423570 7657549481 7657644534 76...
result:
ok 497602 numbers
Test #5:
score: 10
Accepted
time: 1126ms
memory: 124672kb
input:
410863 494517 315605 167 272660 764 1193 76758 165361 315422 61913 359311 69074 75101 91529 41283 181704 199 136706 204701 237333 60064 228582 48986 124350 29453 12587 1269 54671 131785 283380 383 52825 53004 113549 18112 39356 304370 3768 26531 44468 195138 32392 273607 141118 18058 149582 253085 5...
output:
150096 150099 150096 150105 150107 150097 150098 150096 150099 150096 150096 150097 150100 150099 150096 150096 150096 150096 150096 150097 150099 150097 150103 150096 150097 150101 150098 150096 150097 150104 150098 150099 150098 150096 150096 150097 150100 150096 150097 150097 150099 150098 150097...
result:
ok 410863 numbers
Test #6:
score: 10
Accepted
time: 953ms
memory: 125632kb
input:
402237 497417 1 1 49 1 777 2 1328 39 1 17 3 77 3 286 6 1 4 1 1 1 1 149 1 11 6 100 1 1 5 3 1 1192 1 5 9 7 1 75 6 2 7 11 6 2 2 1 2 3751 2 157 1 5 1 41 61 65 1 1 126 10 6 9 1 1 1 4 1 22 1 1 1 4 5 1 53 1 136 11 1 1 1 1 2 1 2 58 1 1 2 1 1 7 4 3 1 1 7 68 1 2 30 2 103 10 1 37 1 88 168 1 18 1 1 2 58 20 1 27...
output:
5746687845 5746687845 5746553712 5746553218 5746553225 5746585507 5746553221 5747023041 5746823025 5746553219 5746570875 5746553218 5746570875 5746553261 5746560400 5746687845 5746565245 5746687845 5746553219 5746991911 5746752912 5746553218 5746553218 5746556674 5746560400 5746553394 5746553218 574...
result:
ok 402237 numbers
Test #7:
score: 10
Accepted
time: 968ms
memory: 121612kb
input:
393273 427651 2 1 243 4 1 9 7 1 1 1 1 81 1 3 18 1 3 1 5 1 1 1 1 1 1 1 38 2 1 1 4 90 5 1 9 1 2 1 38 1 4 42 1 1 121 334 9 5 39 1 1 2 1 2 1 1 1 2 15 1 20 15 1 1 9 1 1 404 38 4 1 1 1 44 48 4 1 2 2 3 665 2 1 1 3 1 7 1 1 2 1 1 2 1 1 124 1 6 12 1 2 1 149 4 1 1 26 10 1 4 1 19 1015 31 2 1 1 17 2 154 1 1 2 5 ...
output:
8301966474 8301768331 8301602635 8301779844 8302110023 8301606182 8301602612 8301934049 8301768331 8301966475 8301768331 8301602747 8301602612 8301784929 8301603992 8301602612 8301627134 8301768331 8301602612 8301768331 8301934049 8301768331 8301944642 8301602612 8301602612 8301768331 8301768815 830...
result:
ok 393273 numbers
Test #8:
score: 10
Accepted
time: 1094ms
memory: 123376kb
input:
495491 499412 6 1 1 1 1 1 1 1 4 1 1 3 93 2 1 17 2 5 151 1 1 2 119 1 9 1 128 4 1 1 1 1 1 1 3 12 48 1 3 1 1 30 1 56 1 2 1 1 4 1 1 1 1 1 7 1 1 2 9 5 1 4 2 1 5 1 3 3 1 1 4 9 1 1 3 26 1 33 1 1 1 209 1 238 41 1 37 2 1 1 1 1 29 2 1 14 2 1 1 2 2 1 40 27 1 1 3 1 3 3 1 1 1 120 4 1 1 1 1 2 1 2 1 1 1 1 8 2 9 17...
output:
18338133934 18329315060 18320724899 18316765889 18317326128 18309440979 18312831504 18308335517 18311317339 18307689629 18304402508 18306540636 18304162417 18306677459 18304537208 18304464941 18305963873 18303674437 18304887513 18303979643 18303020202 18302519263 18304834031 18302285031 18304009526 ...
result:
ok 495491 numbers
Test #9:
score: 10
Accepted
time: 1339ms
memory: 138180kb
input:
485728 479282 16 7 21 1 1423 303 4 746 46 767 553 1 156 268 1 169 1 2 4 6 2299 4 6 55 1500 242 2 1291 1 1 35 1 1 124 41 9 1 3 2 3 4 1 11 4 4 2 2 1 2 5 82 20 49 123 5 1 51 1 328 17 26 9 3 248 1 5 2995 5 6 5 10 147 5 1 8 1 1 71 1 1 43 3 3 7 100 53 2 69 44 3448 45 33 7 1 1 4 28 97 1 1 223 12 6 369 52 1...
output:
4750722741 4750843143 4750719716 4750835661 4750719719 4750719793 4750734038 4750755323 4750720572 4750719814 4750719743 4750719716 4750725640 4750719808 4750951605 4750719884 4750835661 4750878731 4750734038 4750728572 4750719717 4750734038 4750719716 4750720481 4750722081 4750719822 4750753812 475...
result:
ok 485728 numbers
Test #10:
score: 10
Accepted
time: 1359ms
memory: 139320kb
input:
499268 499621 1008 256 6 5 150 15 286 3 1 2 1 1 1 5 4 45 1 1 13 1 1 164 1 8 1 2 12 4 1 4 76 5 4 18 1 1 1 24 4 4 1 1 1 1 1 3 2 21 6 1 1 1 1 166 5 1 1 3 54 3 1 5 2 1 1 4 6 1 24 1 990 2 1 2 14 10 4 1 1 19 1 10 1 1 614 1 56 2 188 1 12 6 2 1 2 100 1 1 543 6 28 18 1 1 1 1 5 1 51 163 2 6 915 21 1 2 3 3 1 4...
output:
8031648684 8031648740 8032134205 8031660033 8031648684 8031651545 8031648684 8031829474 8031807614 8031688261 8031807995 8031807648 8031648684 8031660033 8031700655 8031649412 8031810286 8031648684 8031995276 8031807614 8031807614 8031648999 8031648684 8031654940 8031807614 8031727841 8031652559 803...
result:
ok 499268 numbers