QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#741773#6127. Kawa ExamACR01WA 0ms26104kbC++144.3kb2024-11-13 15:12:022024-11-13 15:12:04

Judging History

你现在查看的是最新测评结果

  • [2024-11-13 15:12:04]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:26104kb
  • [2024-11-13 15:12:02]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+5;
int n,m,a[N],b[N];
struct EDGE{int to,num;};
vector<EDGE> e[N],g[N];
vector<int> nd[N];
int cnt,tot,top;
int dfn[N],low[N],stk[N],col[N];
void Tarjan(int pos,int lst)
{
	stk[++top]=pos;
	dfn[pos]=low[pos]=++cnt;
	for(int i=0;i<e[pos].size();i++)
	{
		int v=e[pos][i].to,num=e[pos][i].num;
		if((num^1)==lst) continue;
		if(!dfn[v]){
			Tarjan(v,num);
			low[pos]=min(low[pos],low[v]);
		}
		else if(!col[v]) low[pos]=min(low[pos],dfn[v]);
	}
	if(low[pos]==dfn[pos])
	{
		col[pos]=++tot;
		nd[tot].push_back(pos);
		while(stk[top]!=pos)
		{
			nd[tot].push_back(stk[top]);
			col[stk[top]--]=tot;
		}
		top--;
	}
}
bool used[N];
int ct,hs[N],sz[N],trk[N],rt[N],root;
void dfs1(int pos,int fa)
{
	rt[pos]=root;
	sz[pos]=nd[pos].size();
	dfn[pos]=++ct;
	trk[ct]=pos;
	for(int i=0;i<g[pos].size();i++)
	{
		int v=g[pos][i].to;
		if(v==fa) continue;
		dfs1(v,pos);
		sz[pos]+=sz[v];
		if(sz[hs[pos]]<sz[v]) hs[pos]=v;
	}
}
int nmx,mx[N],ctt[N];
int pla[N],ans[N];
bool vis[N];
void del(int col){
	pla[ctt[col]]--;
	ctt[col]--;
	pla[ctt[col]]++;
	if(pla[nmx]==0) nmx--;
}
void add(int col){
	pla[ctt[col]]--;
	ctt[col]++;
	if(ctt[col]>nmx) nmx=ctt[col];
	pla[ctt[col]]++;
}
int tt;
void dfs2(int pos,int fa,bool save)
{
	for(int i=0;i<g[pos].size();i++)
	{
		int v=g[pos][i].to;
		if(v==fa||v==hs[pos]) continue;
		dfs2(v,pos,false);
	} 
	if(hs[pos]) dfs2(hs[pos],pos,true);
	for(int i=0;i<g[pos].size();i++)
	{
		int v=g[pos][i].to;
		if(v==fa||v==hs[pos]) continue;
		for(int j=dfn[v];j<=dfn[v]+sz[v]-1;j++)
		{
			int now=trk[j];
			for(int k=0;k<nd[now].size();k++)
			{
				int col=a[nd[now][k]];
				add(col);
			}
		}
	}
	for(auto now:nd[pos])
	{
		int col=a[now];
		add(col);
	}
	mx[pos]=nmx;
	if(save==false){
		for(int i=dfn[pos];i<=dfn[pos]+sz[pos]-1;i++)
		{
			for(auto v:nd[trk[i]]){
				int col=a[v];
				del(col);
			}
		}
	}
}
void dfs3(int pos,int fa,bool save,int nm)
{
	vis[pos]=true;
	int ppk=0;
	for(int i=0;i<g[pos].size();i++)
	{
		int v=g[pos][i].to,num=g[pos][i].num;
		if(v==hs[pos]) ppk=num;
		if(v==fa||v==hs[pos]) continue;
		dfs3(v,pos,false,num);
	} 
	if(hs[pos])	dfs3(hs[pos],pos,true,ppk);
	for(int i=0;i<g[pos].size();i++)
	{
		int v=g[pos][i].to;
		if(v==fa||v==hs[pos]) continue;
		for(int j=dfn[v];j<=dfn[v]+sz[v]-1;j++)
		{
			int now=trk[j];
			for(int k=0;k<nd[now].size();k++)
			{
				int col=a[nd[now][k]];
				del(col);
			}
		}
	}
	for(auto now:nd[pos])
	{
		int col=a[now];
		del(col);
	}
	ans[nm]=nmx+mx[pos]+tt-mx[rt[root]];
	if(save==false){
		for(int i=dfn[pos];i<=dfn[pos]+sz[pos]-1;i++)
		{
			for(auto v:nd[trk[i]]){
				int col=a[v];
				add(col);
			}
		}
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		cin>>n>>m;
		for(int i=1;i<=n;i++)
			cin>>a[i],b[i]=a[i];
		sort(b+1,b+n+1);
		int mm=unique(b+1,b+n+1)-b-1;
		for(int i=1;i<=n;i++)
			a[i]=lower_bound(b+1,b+mm+1,a[i])-b;
		cnt=tot=top=tt=root=ct=0;
		for(int i=0;i<=n;i++)
		{
			e[i].clear();
			g[i].clear();
			nd[i].clear();
			col[i]=dfn[i]=low[i]=stk[i]=sz[i]=0;
			hs[i]=trk[i]=rt[i]=mx[i]=ctt[i]=0;
			pla[i]=0;
			vis[i]=false;
		}
		for(int i=0;i<=m;i++)
			ans[i]=used[i]=0;
		for(int i=1;i<=m;i++)
		{
			int u,v;
			cin>>u>>v;
			e[u].push_back(EDGE{v,(i<<1)});
			e[v].push_back(EDGE{u,(i<<1)|1});
		}
		for(int i=1;i<=n;i++)
			if(!dfn[i]) Tarjan(i,0);
		for(int i=1;i<=n;i++)
		{
			for(int j=0;j<e[i].size();j++)
			{
				int v=e[i][j].to,num=e[i][j].num/2;
				if(col[v]==col[i]){
					used[num]=true;
					continue;
				}
				g[col[i]].push_back(EDGE{col[v],num});
			}
		}
		for(int i=1;i<=n;i++)
		{
			if(!sz[col[i]]){
				root=col[i];
				nmx=pla[0]=0;
				dfs1(col[i],0);
				dfs2(col[i],0,false);
				tt+=mx[col[i]];
			}
		}
		for(int i=1;i<=n;i++)
		{
			if(vis[col[i]]) continue;
			int now=col[i];
			pla[0]=0;
			nmx=0;
			for(int j=dfn[now];j<=dfn[now]+sz[now]-1;j++)
			{
				for(auto v:nd[trk[j]])
					add(a[v]);
			}
			dfs3(col[i],0,true,0);
		}
		for(int i=1;i<=m;i++)
		{
			if(used[i]==true) cout<<tt<<" ";
			else cout<<ans[i]<<" ";
		}
		cout<<"\n";
	}
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 26104kb

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 '