QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#375211 | #6127. Kawa Exam | murphy | AC ✓ | 2299ms | 60152kb | C++14 | 4.3kb | 2024-04-02 23:43:40 | 2024-04-02 23:43:40 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int INF=0x3f3f3f3f, N=3e5+10;
const double eps=1e-6, PI=acos(-1);
int cases, n, m;
int dfn[N], low[N], dfs_cnt, stk[N], top, e_dcc, blk[N];
int col[N];
struct Node
{
int a, cnt[N], num[N];
void clear()
{
for (int i=1; i<=n; i++) cnt[col[i]]=0, num[i]=0;
a=0;
}
void add(int x)
{
if (cnt[x]) num[cnt[x]]--;
cnt[x]++; num[cnt[x]]++;
a=max(a, cnt[x]);
}
void del(int x)
{
num[cnt[x]]--, cnt[x]--;
if (cnt[x]) num[cnt[x]]++;
a=num[a]>0 ? a: a-1;
}
}D1, D2;
struct Edge
{
int v, w, nxt;
}e[N];
int h[N], ecnt;
void add(int u, int v, int w)
{
e[ecnt].nxt=h[u], e[ecnt].v=v, e[ecnt].w=w, h[u]=ecnt++;
}
void tarjan(int u,int from)
{
dfn[u]=low[u]=++dfs_cnt;
stk[++top]=u;
for (int i=h[u];i!=-1;i=e[i].nxt)
{
int v=e[i].v;
if (!dfn[v])
{
tarjan(v,i);
low[u]=min(low[u],low[v]);
}
else if (i!=(from^1)) low[u]=min(low[u], dfn[v]);
}
if (low[u]==dfn[u])
{
int y;
e_dcc++;
do
{
y=stk[top--];
blk[y]=e_dcc;
} while (y!=u);
}
}
vector<int> G[N], nums[N], E[N];
int siz[N], son[N], id[N], idx=0, L[N], R[N];
void dfs1(int u, int fa)
{
idx++; siz[u]=(int)nums[u].size(), id[idx]=u;
L[u]=idx;
for (auto v:G[u])
{
if (v==fa) continue;
dfs1(v, u);
siz[u]+=siz[v];
if (siz[son[u]] < siz[v]) son[u]=v;
}
R[u]=idx;
}
int ans[N];
void dfs2(int u, int fa, int keep, int all, int edge)
{
for (int i=0; i<(int)G[u].size(); i++)
{
int v=G[u][i], w=E[u][i];
if (v==fa || v==son[u]) continue;
dfs2(v, u, 0, all, w);
}
for (int i=0; i<(int)G[u].size(); i++)
{
int v=G[u][i], w=E[u][i];
if (v==son[u]) dfs2(v, u, 1, all, w);
}
for (int i=0; i<(int)G[u].size(); i++)
{
int v=G[u][i], w=E[u][i];
if (v==fa || v==son[u]) continue;
for (int j=L[v]; j<=R[v]; j++) for (auto x:nums[id[j]]) D1.add(x), D2.del(x);
}
for (auto x:nums[u]) D1.add(x), D2.del(x);
if (edge) ans[edge]=D1.a+D2.a-all;
if (!keep)
{
for (int i=L[u]; i<=R[u]; i++)
for (auto x:nums[id[i]]) D1.del(x), D2.add(x);
}
}
void init()
{
memset(h, -1, sizeof(h)); ecnt=0;
memset(dfn, 0, sizeof(dfn)); memset(low, 0, sizeof(low)); dfs_cnt=0;
top=0; memset(blk, 0, sizeof(blk)); e_dcc=0;
idx=0; memset(son, 0, sizeof(son)); memset(L, 0, sizeof(L));
memset(ans, 0, sizeof(ans));
}
void mian(int dt)
{
init();
cin>>n>>m;
for (int i=1; i<=n; i++) cin>>col[i];
for (int i=1; i<=m; i++)
{
int x, y; cin>>x>>y; add(x, y, i); add(y, x, i);
}
for (int i=1; i<=n; i++) if (!dfn[i]) tarjan(i, -1);
for (int i=1; i<=e_dcc; i++) nums[i].clear(), G[i].clear(), E[i].clear();
for (int i=1; i<=n; i++) nums[blk[i]].push_back(col[i]);
for (int i=1; i<=n; i++)
{
for (int j=h[i]; j!=-1; j=e[j].nxt)
{
int v=e[j].v, w=e[j].w;
if (blk[i] != blk[v])
{
G[blk[i]].push_back(blk[v]); E[blk[i]].push_back(w);
}
}
}
for (int i=1; i<=e_dcc; i++)
{
if (!L[i])
{
dfs1(i, -1);
for (int j=L[i]; j<=R[i]; j++)
{
for (auto x:nums[id[j]])
{
D2.add(x);
}
}
ans[0]+=D2.a;
dfs2(i, -1, 0, D2.a, 0);
for (int j=L[i]; j<=R[i]; j++)
{
for (auto x:nums[id[j]])
{
D2.del(x);
}
}
}
}
for (int i=1; i<m; i++) cout<<ans[i]+ans[0]<<" ";
cout<<ans[m]+ans[0]<<endl;
D1.clear(), D2.clear();
}
int main()
{
cin>>cases;
for (int i=1; i<=cases; i++) mian(i);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 40184kb
input:
3 7 5 1 2 1 2 1 2 1 1 2 1 3 2 4 5 6 5 7 3 3 1 2 3 1 2 1 3 2 3 2 3 12345 54321 1 2 1 2 1 1
output:
6 5 5 5 4 1 1 1 1 1 1
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 2299ms
memory: 59424kb
input:
5557 2 7 79960 79960 2 2 1 1 1 1 2 2 1 1 2 1 1 2 9 8 21881 70740 70740 21881 22458 22458 639 21881 70740 3 3 1 6 5 8 7 5 5 7 2 3 5 1 7 6 6 7 13064 20716 6746 13064 6746 69225 5 5 4 1 4 1 1 6 4 5 3 2 3 2 8 4 45146 14400 45146 45146 14400 72969 14400 45146 8 6 1 3 4 6 8 3 18 13 48132 37949 92338 92338...
output:
2 2 2 2 2 2 2 6 6 7 6 6 6 6 6 3 3 3 4 4 3 3 7 7 7 7 9 9 9 8 9 8 9 8 9 9 10 9 9 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 7 8 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 9 10 9 16 16 16 16 16 17 16 16 10 10 11 10 12 11 10 10 10 10 10 10 10 12 10 10 10 10 10 11 10 9 9 9 9 9 9 9 9 9 9 9 9 9 10 ...
result:
ok 5557 lines
Test #3:
score: 0
Accepted
time: 624ms
memory: 60152kb
input:
10 100000 99999 3983 3983 20157 97983 20157 20157 3983 3983 97983 20157 20157 3983 97983 20157 3983 20157 20157 3983 3983 3983 97983 97983 20157 3983 3983 97983 20157 97983 20157 97983 3983 97983 97983 3983 20157 3983 20157 20157 97983 3983 3983 3983 3983 97983 97983 3983 97983 97983 3983 20157 3983...
output:
33392 33393 33393 33393 33393 33392 33392 33393 33393 33393 33392 33393 33393 33392 33393 33393 33392 33392 33392 33393 33393 33393 33392 33392 33393 33393 33393 33393 33393 33392 33393 33393 33392 33393 33392 33393 33393 33393 33392 33392 33392 33392 33393 33393 33392 33393 33393 33392 33393 33392 ...
result:
ok 10 lines
Test #4:
score: 0
Accepted
time: 620ms
memory: 55720kb
input:
10 100000 99999 27534 27534 3780 3780 27534 53544 27534 3780 3780 53544 53544 27534 53544 53544 3780 3780 3780 3780 53544 27534 3780 3780 53544 27534 27534 53544 27534 27534 53544 27534 27534 27534 3780 27534 27534 3780 3780 3780 27534 53544 3780 53544 27534 3780 3780 3780 27534 27534 27534 3780 275...
output:
33613 33601 33601 33600 33600 33601 33601 33601 33600 33601 33600 33600 33601 33601 33601 33601 33601 33601 33600 33600 33601 33601 33601 33601 33600 33601 33601 33600 33601 33600 33601 33600 33601 33601 33601 33601 33600 33601 33601 33601 33601 33601 33601 33601 33601 33601 33600 33601 33600 33601 ...
result:
ok 10 lines
Test #5:
score: 0
Accepted
time: 640ms
memory: 54412kb
input:
10 100000 99999 92499 92270 92270 92499 92499 92499 92270 54017 92270 92270 92270 54017 54017 54017 54017 92270 92499 54017 92270 54017 92499 92499 92270 92270 54017 54017 54017 54017 92270 92270 92499 54017 54017 92499 92499 54017 92270 92270 54017 92499 92270 92270 54017 54017 54017 92499 92499 54...
output:
33506 33482 33507 33482 33508 33483 33508 33483 33508 33483 33507 33483 33506 33483 33505 33483 33503 33483 33503 33482 33504 33483 33505 33483 33504 33483 33502 33483 33501 33483 33500 33482 33502 33483 33500 33483 33501 33482 33502 33483 33501 33483 33500 33482 33500 33483 33498 33483 33499 33483 ...
result:
ok 10 lines
Test #6:
score: 0
Accepted
time: 652ms
memory: 55312kb
input:
10 100000 99999 76207 76207 88551 88551 98176 76207 98176 88551 88551 98176 88551 76207 76207 98176 98176 76207 76207 88551 76207 88551 76207 88551 88551 76207 88551 76207 98176 88551 76207 98176 88551 88551 76207 88551 98176 88551 76207 76207 98176 88551 76207 98176 76207 88551 88551 88551 88551 76...
output:
33484 33484 33476 33484 33477 33485 33476 33485 33477 33485 33477 33486 33477 33484 33477 33485 33476 33485 33476 33485 33476 33483 33477 33483 33477 33485 33476 33485 33477 33485 33476 33487 33476 33487 33476 33486 33477 33486 33476 33486 33477 33486 33476 33486 33476 33486 33477 33487 33477 33487 ...
result:
ok 10 lines
Test #7:
score: 0
Accepted
time: 645ms
memory: 53432kb
input:
10 100000 99999 70486 49904 70486 49904 87935 49904 49904 87935 87935 49904 49904 87935 49904 87935 87935 70486 49904 87935 87935 49904 70486 87935 49904 70486 87935 87935 49904 49904 49904 87935 70486 70486 70486 49904 70486 87935 87935 87935 70486 87935 70486 49904 87935 49904 49904 87935 70486 87...
output:
33491 33486 33489 33486 33489 33486 33489 33486 33487 33486 33487 33486 33486 33485 33486 33486 33486 33486 33485 33486 33485 33485 33486 33486 33485 33486 33485 33486 33485 33485 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33485 33486 33485 ...
result:
ok 10 lines
Test #8:
score: 0
Accepted
time: 622ms
memory: 52988kb
input:
10 100000 99999 98004 33580 98004 98004 98004 92291 92291 98004 98004 92291 92291 33580 98004 92291 33580 98004 98004 33580 98004 92291 92291 33580 92291 92291 98004 33580 98004 33580 33580 98004 33580 92291 33580 33580 92291 92291 92291 98004 33580 98004 92291 92291 33580 92291 98004 98004 92291 92...
output:
33462 33463 33421 33463 33422 33465 33421 33463 33422 33464 33422 33462 33422 33464 33421 33464 33422 33464 33422 33465 33422 33463 33422 33462 33422 33463 33422 33465 33421 33464 33422 33464 33422 33463 33422 33463 33421 33463 33421 33462 33422 33460 33422 33461 33421 33461 33422 33460 33422 33459 ...
result:
ok 10 lines