QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#664361#6127. Kawa ExamSiilhouetteAC ✓406ms33456kbC++234.6kb2024-10-21 20:18:342024-10-21 20:18:34

Judging History

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

  • [2024-10-21 20:18:34]
  • 评测
  • 测评结果:AC
  • 用时:406ms
  • 内存:33456kb
  • [2024-10-21 20:18:34]
  • 提交

answer

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<map>
using namespace std;

const int N=200010;
int head[N],ver[N<<1],suiv[N<<1],dfn[N],low[N],c[N],vis[N],cnt[N],siz[N],son[N],edge[N<<1],eans[N<<1];
int n,m,tot,num,dcc,ans,a[N];
bool pont[N];
int hc[N],vc[N<<1],sc[N<<1],ec[N<<1],tc;
vector<int>inc[N];

#define DEBUG 0
#define QOJ 0

inline void add_c(int x,int y,int z)
{
#if DEBUG
	cout<<"add_c "<<x<<" "<<y<<" "<<z<<endl;
#endif
	vc[++tc]=y;
	ec[tc]=z;
	sc[tc]=hc[x];
	hc[x]=tc;
}

inline void add(int x,int y,int z)  //edge存的是边的编号
{
	ver[++tot]=y;
	edge[tot]=z;
	suiv[tot]=head[x];
	head[x]=tot;
}

inline void tarjan_pont(int x,int inedge)
{
	dfn[x]=low[x]=++num;
	for(int i=head[x];i;i=suiv[i])
	{
		int y=ver[i];
		if(!dfn[y])
		{
			tarjan_pont(y,i);
			low[x]=min(low[x],low[y]);
			if(low[y]>dfn[x])
				pont[i]=pont[i^1]=1;
		}
		else if(i!=(inedge^1))
			low[x]=min(low[x],dfn[y]);
	}
}

inline void dfs_eDCC(int x)
{
	c[x]=dcc;
	inc[dcc].push_back(a[x]);
	for(int i=head[x];i;i=suiv[i])
	{
		int y=ver[i];
		if(c[y] || pont[i])continue;
		dfs_eDCC(y);
	}
}

inline void init(int x)
{
	for(int i=1;i<=x;i++)
		head[i]=hc[i]=dfn[i]=low[i]=c[i]=vis[i]=cnt[i]=siz[i]=son[i]=0,
		inc[i].clear();
	for(int i=1;i<=tot;i++)
		pont[i]=0;
	tot=tc=1;
	num=dcc=ans=0;
}

struct Barrel{  //计算众数个数
	int maxi,b[N],cntb[N];
	Barrel(){maxi=0;}
	inline void insert(int x)
	{
		//cout<<"insert "<<x<<endl;
		if(b[x])cntb[b[x]]--;
		b[x]++;
		cntb[b[x]]++;
		if(b[x]>maxi)maxi=b[x];
		//cout<<"maxi "<<maxi<<endl;
	}

	inline void del(int x)
	{
		cntb[b[x]]--;
		b[x]--;
		if(b[x])cntb[b[x]]++;
		while(!cntb[maxi]&&maxi)maxi--;
	}

	inline int getnum(){return maxi;}

}t1,t2;

//缩点后的siz应该是 真实的点数 因为DSUon tree的时候需要遍历所有的点 
inline void dfs1(int x,int fa=-1,int maxison=-1)  
{
	vis[x]=1;siz[x]=inc[x].size();
	for(int i=hc[x];i;i=sc[i])
	{
		int y=vc[i];
		if(y==fa)continue;
		dfs1(y,x);siz[x]+=siz[y];
		if(siz[y]>maxison)
			maxison=siz[y],son[x]=y;
	}
}

//将这个数里的所有点放入桶 / 从桶里拿出来
inline void dfs2(int x,int val,int fa=-1)
{
	for(auto pos:inc[x])
	{
		if(val==1)t1.insert(pos);
		else t1.del(pos);
	}	
	for(int i=hc[x];i;i=sc[i])
	{
		int y=vc[i];
		if(y==fa)continue;
		dfs2(y,val,x);
	}
}

inline void change(int x,int forbid,int fa,int val)
{
	if(val==1)
		for(auto pos:inc[x])
			t1.del(pos),t2.insert(pos);
	else 
		for(auto pos:inc[x])
			t1.insert(pos),t2.del(pos);
	for(int i=hc[x];i;i=sc[i])
	{
		int y=vc[i];
		if(y==forbid||y==fa)continue;
		change(y,forbid,x,val);
	}
}

inline void dfs3(int x,int fa_edge,int fa,int op=0)
{
	vis[x]=0;
	int son_edge=-1;
	for(int i=hc[x];i;i=sc[i])
	{
		int y=vc[i];
		if(y==son[x])son_edge=i;
		if(y==son[x]||y==fa)continue;
		dfs3(y,i,x);
	}
	if(son[x])dfs3(son[x],son_edge,x,1);
	change(x,son[x],fa,1);
	if(fa_edge!=-1)
	{
#if DEBUG
		cout<<"dfs3 "<<x<<" "<<ec[fa_edge]<<" "<<ans<<" "<<t1.getnum()<<" "<<t2.getnum()<<endl;
#endif
		eans[ec[fa_edge]]=ans+t1.getnum()+t2.getnum();
	}
	if(!op)change(x,0,fa,-1);
}

int main()
{
	int T;
	scanf("%d",&T);
	for(int _=1;_<=T;_++)
	{
		ans=0;
		tot=tc=1;
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);

		for(int i=1,x,y;i<=m;i++)
		{
			scanf("%d%d",&x,&y);
			add(x,y,i),add(y,x,i);
			//if(_==11&&i>=6)cout<<x<<"->"<<y<<"#";
		}
		for(int i=1;i<=n;i++)
			if(!dfn[i])tarjan_pont(i,0);
		for(int i=1;i<=n;i++)
			if(!c[i])dcc++,dfs_eDCC(i);
		for(int i=2;i<=tot;i++)
		{
			int x=ver[i],y=ver[i^1];
			if(c[x]==c[y])continue;
			add_c(c[x],c[y],edge[i]);
		}	
		for(int i=1;i<=dcc;i++)
		{
			if(vis[i])continue;
#if DEBUG
			//cout<<"i "<<i<<" "<<cnt[i]<<endl;
			//cout<<"t "<<t1.getnum()<<" "<<t2.getnum()<<endl;
#endif
			dfs1(i);
			dfs2(i,1);
			cnt[i]=t1.getnum();
			dfs2(i,-1);
			ans+=cnt[i];
#if DEBUG
			cout<<"i "<<i<<" "<<cnt[i]<<endl;
#endif
		}
		for(int i=1;i<=m;i++)
			eans[i]=-1;
		for(int i=1;i<=dcc;i++)
		{
			if(!vis[i])continue;

			ans-=cnt[i];
			dfs2(i,1);
			dfs3(i,-1,-1);
			ans+=cnt[i];
			dfs2(i,-1);
		}
		for(int i=1;i<=m;i++)
		{
			if(eans[i]!=-1)printf("%d",eans[i]);
			else printf("%d",ans);
			if(i<m)putchar(' ');
		}
		putchar('\n');
		init(n);
	}
	return 0;
}

/*
1
3 3
1 2 3
1 2
1 3
2 3

1
19 8
3 2 2 3 1 2 1 3 2 
3 2 2 3 3 3 2 3 2 1
6 2
13 3
14 3
11 6
7 4
9 4
17 7
12 18


16 16 16 16 16 17 16 16
*/

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 20352kb

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: 406ms
memory: 33456kb

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: 306ms
memory: 29472kb

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: 302ms
memory: 28460kb

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: 347ms
memory: 29952kb

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: 338ms
memory: 30204kb

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: 339ms
memory: 29592kb

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: 352ms
memory: 27756kb

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