QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#385887#6127. Kawa ExamliyujiangwxAC ✓487ms37424kbC++143.9kb2024-04-11 09:38:142024-04-11 09:38:15

Judging History

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

  • [2024-04-11 09:38:15]
  • 评测
  • 测评结果:AC
  • 用时:487ms
  • 内存:37424kb
  • [2024-04-11 09:38:14]
  • 提交

answer

#include<bits/stdc++.h>
#define rs now<<1|1
#define ls now<<1

using namespace std;

inline int read(){
	int x=0,f=0;
	char c=getchar();
	while(c<'0'||c>'9'){f|=(c=='-');c=getchar();}
	while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}
	return f?-x:x;
}

const int N=3e5+10;
const int Max=3e5+10;
struct edge{
	int to,ne,id;
	edge(int to=0,int ne=0,int id=0):
		to(to),ne(ne),id(id){;}
}a[Max<<1];
int h[N],cnt=1,tot,dfn[N],idx,low[N],stk[N],col[N],ans[N],res;
int top,n,m,T,bel[N],fr[N],to[N],siz[N],son[N],Son,ys[N],Tot;
vector < int > vec[N];

struct SegmentTree{
	int maxx[N<<2];
	void pushup(int now){maxx[now]=max(maxx[ls],maxx[rs]);};
	void build(int now,int l,int r){
		maxx[now]=0;
		if(l==r) return ;
		int mid=(l+r)>>1;
		build(ls,l,mid);
		build(rs,mid+1,r);
	}
	void update(int now,int l,int r,int to,int v){
		if(l==r) {
			maxx[now]+=v;
			return ;
		}
		int mid=(l+r)>>1;
		if(mid>=to) update(ls,l,mid,to,v);
		else update(rs,mid+1,r,to,v);
		pushup(now);
	}
	int get(){return maxx[1];}
	void clear(){build(1,1,Tot+5);}
	void change(int to,int v){update(1,1,Tot+5,to,v);}
}val,rev;

void Clear(vector < int > &v){
	vector < int > tmp;
	swap(tmp,v);
}

void clear(){
	val.clear();rev.clear();
	for(int i=1;i<=m;++i) ans[i]=0;
	for(int i=1;i<=cnt;++i) a[i]=edge();
	for(int i=1;i<=tot;++i) Clear(vec[i]);
	for(int i=1;i<=n+tot;++i) stk[i]=bel[i]=dfn[i]=low[i]=h[i]=siz[i]=son[i]=0;
	cnt=1;Tot=idx=top=tot=res=Son=0;
}

void Add(int x,int y,int z){
	if(cnt>=(Max<<1)) exit(0);
	a[++cnt]=edge(y,h[x],z);
	h[x]=cnt;
}

void tarjan(int x,int las){
	dfn[x]=low[x]=++idx;
	if(top>=N) exit(0);
	stk[++top]=x;
	for(int i=h[x];i;i=a[i].ne){
		if((las^1)==i) continue;
		if(!dfn[a[i].to]) {
			tarjan(a[i].to,i);
			low[x]=min(low[x],low[a[i].to]);
		}else low[x]=min(low[x],dfn[a[i].to]);
	}
	if(dfn[x]==low[x]){
		++tot;
		if(tot>=N) exit(0);
		while(stk[top+1]!=x) vec[tot].emplace_back(stk[top--]);
	}
}

void dfs1(int x,int y){
	siz[x]=vec[x-n].size();
	for(int i=h[x];i;i=a[i].ne) {
		if(a[i].to==y) continue;
		dfs1(a[i].to,x);
		siz[x]+=siz[a[i].to];
		son[x]=siz[son[x]]>siz[a[i].to]?son[x]:a[i].to;
	}
}

void ad(int x){val.change(x,1);rev.change(x,-1);}
void del(int x){val.change(x,-1);rev.change(x,1);}

void adds(int x,int y,int opt){
	for(auto i:vec[x-n]) 
		if(opt) ad(col[i]);
		else del(col[i]);
	for(int i=h[x];i;i=a[i].ne) if(a[i].to!=y&&a[i].to!=Son) adds(a[i].to,x,opt);
}

void dfs2(int x,int y,int opt,int down){
	int fa=0;
	for(int i=h[x];i;i=a[i].ne) {
		if(a[i].to!=son[x]&&a[i].to!=y) dfs2(a[i].to,x,0,down);
		else if(a[i].to==y) fa=a[i].id;
	}
	if(son[x]){
		dfs2(son[x],x,1,down);
		Son=son[x];
	}
	adds(x,y,1);
	ans[fa]=val.get()+rev.get()-down;
	if(!opt){Son=0;adds(x,y,0);}
}

void dfs3(int x,int y,int opt){
	for(auto i:vec[x-n]) {
		int tmp=col[i];
		if(opt) rev.change(tmp,1);
		else rev.change(tmp,-1);
	}
	for(int i=h[x];i;i=a[i].ne) if(a[i].to!=y) dfs3(a[i].to,x,opt);
}

int main(){
	T=read();
	while(T--){
		n=read();m=read();
		for(int i=1;i<=n;++i) {
			if(Tot>=n) return 0;
			ys[++Tot]=col[i]=read();
		}
		sort(ys+1,ys+1+Tot);Tot=unique(ys+1,ys+1+Tot)-ys-1;
		for(int i=1;i<=n;++i) col[i]=lower_bound(ys+1,ys+1+Tot,col[i])-ys;
		for(int i=1,x,y;i<=m;++i){
			fr[i]=x=read();
			to[i]=y=read();
			Add(x,y,i);
			Add(y,x,i);
		}
		for(int i=1;i<=n;++i) if(!dfn[i]) tarjan(i,cnt<<2);
		for(int i=1;i<=tot;++i) for(auto j:vec[i]) bel[j]=i+n;
		for(int i=1;i<=m;++i) 
			if(bel[fr[i]]!=bel[to[i]]) {
				if(!bel[fr[i]]||!bel[to[i]]) return 0;
				Add(bel[fr[i]],bel[to[i]],i);
				Add(bel[to[i]],bel[fr[i]],i);
			}
		for(int i=n+1;i<=n+tot;++i) 
			if(!siz[i]) {
				dfs1(i,0);
 				dfs3(i,0,1);
				int tmp=rev.get();
				res+=tmp;
				dfs2(i,0,0,tmp);
				dfs3(i,0,0);
			}
		for(int i=1;i<=m;++i) {
			printf("%d",ans[i]+res);
			if(i!=m) putchar(' ');
		}
		if(T) putchar('\n');
		clear(); 
	}
	return 0;
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 29680kb

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: 487ms
memory: 37424kb

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: 261ms
memory: 35552kb

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: 257ms
memory: 34036kb

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: 324ms
memory: 35888kb

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: 316ms
memory: 36044kb

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: 314ms
memory: 35780kb

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: 324ms
memory: 33924kb

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