QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#373694 | #6127. Kawa Exam | murphy | WA | 5ms | 35600kb | C++14 | 4.2kb | 2024-04-01 23:39:26 | 2024-04-01 23:39:27 |
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 (son[u] && v==son[u]) dfs2(son[u], u, 1, all, w);
else if (v!=fa) 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==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()
{
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<<endl;
D1.clear(), D2.clear();
}
int main()
{
cin>>cases;
while (cases--) mian();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 5ms
memory: 35600kb
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:
wrong answer 1st lines differ - expected: '6 5 5 5 4', found: '6 5 5 5 4 '