QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#151636#6329. Colorful GraphForever_Young#WA 491ms5716kbC++143.6kb2023-08-27 11:56:312023-08-27 11:56:32

Judging History

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

  • [2023-08-27 11:56:32]
  • 评测
  • 测评结果:WA
  • 用时:491ms
  • 内存:5716kb
  • [2023-08-27 11:56:31]
  • 提交

answer

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;++i)
#define per(i,n) for(int i=n;i>=1;--i)
#define pb push_back
#define mp make_pair
#define stack stck  
#define N 7010
#define M 7100
#define inf 1000000007
struct EDGE { int adj, next; } edge[M];
int n, m, top, gh[N];
int dfn[N], low[N], cnt, ind, stop, instack[N], stack[N], belong[N];
void addedge(int x, int y) {
	edge[++top].adj = y;
	edge[top].next = gh[x];
	gh[x] = top;
}
void tarjan(int x) {
	dfn[x] = low[x] = ++ind;
	instack[x] = 1; stack[++stop] = x;
	for (int p=gh[x]; p; p=edge[p].next)
		if (!dfn[edge[p].adj]) {
			tarjan(edge[p].adj);
			low[x] = min(low[x], low[edge[p].adj]);
		} else if (instack[edge[p].adj]) {
			low[x] = min(low[x], dfn[edge[p].adj]);
		}
	if (dfn[x] == low[x]) {
		++cnt; int tmp=0;
		while (tmp!=x) {
			tmp = stack[stop--];
			belong[tmp] = cnt;
			instack[tmp] = 0;
		}
	}
}
pair<int,int> save[M];
set<pair<int,int> > dic; //duplicate edges

namespace flow
{
	struct EDGE { int adj, w, next; } edge[10*M];
	int n, top, gh[2*N], nrl[2*N] , S, T;
	void addedge(int x, int y, int w) {
		//cout<<"fuck: "<<x<<" "<<y<<" "<<w<<endl;
		edge[++top].adj = y;
		edge[top].w = w;
		edge[top].next = gh[x];
		gh[x] = top;
		edge[++top].adj = x;
		edge[top].w = 0;
		edge[top].next = gh[y];
		gh[y] = top;
	}
	int dist[2*N], q[2*N];
	int bfs() {
		memset(dist, 0, sizeof(dist));
		q[1] = S; int head = 0, tail = 1; dist[S] = 1;
		while (head != tail) {
			int x = q[++head];
			for (int p=gh[x]; p; p=edge[p].next)
				if (edge[p].w && !dist[edge[p].adj]) {
					dist[edge[p].adj] = dist[x] + 1;
					q[++tail] = edge[p].adj;
				}
		}
		return dist[T];
	}
	int dinic(int x, int delta) {
		if (x==T) return delta;
		for (int& p=nrl[x]; p && delta; p=edge[p].next)
			if (edge[p].w && dist[x]+1 == dist[edge[p].adj]) {
				int dd = dinic(edge[p].adj, min(delta, edge[p].w));
				if (!dd) continue;
				edge[p].w -= dd;
				edge[p^1].w += dd;
				return dd;
			}
		return 0;
	}
	int work() {
		int ans = 0;
		while (bfs()) {
			memcpy(nrl, gh, sizeof(gh));
			int t; while (t = dinic(S, inf)) ans += t;
		}
		return ans;
	}
}

int col[N],father[N];
int getfather(int x)
{
	if (father[x]==x)return x;
	return father[x]=getfather(father[x]);
}
int flowedge[M];
int main()
{
	cin>>n>>m;
	rep(i,m)
	{
		scanf("%d%d",&save[i].first,&save[i].second);
		addedge(save[i].first,save[i].second);
	}
	
	//SCC
	rep(i,n)if (!dfn[i])tarjan(i);
	//Flow
	flow::top=1; 
	flow::S=0; 
	flow::T=2*n+1;
	rep(i,cnt)
	{
		flow::addedge(flow::S,i,1);
		flow::addedge(n+i,i,inf);
		flow::addedge(n+i,flow::T,1);
	}
	dic.clear();
	rep(i,m)flowedge[i]=-1;
	rep(i,m)
		if (belong[save[i].first]!=belong[save[i].second])
		{
			int x=belong[save[i].first];
			int y=belong[save[i].second];
			if (dic.find(mp(x,y))!=dic.end())continue;
			dic.insert(mp(x,y));
			flow::addedge(x,n+y,inf);
			flowedge[i]=flow::top;
		}
	flow::work();
	rep(i,cnt)father[i]=i;
	rep(i,m)
	if (flowedge[i]!=-1)
	{
		//cout<<belong[save[i].first]<<" "<<n+belong[save[i].second]<<" "<<flow::edge[flowedge[i]].w<<endl;
		if (flow::edge[flowedge[i]].w==0)continue;
		int x=getfather(belong[save[i].first]);
		int y=getfather(belong[save[i].second]);
		if (x<y)swap(x,y);
		father[x]=y;
	}
	int cur=0;
	rep(i,cnt)
	if (getfather(i)==i)col[i]=++cur;
	else col[i]=col[father[i]];

	rep(i,n)printf("%d ",col[belong[i]]);
	puts("");

	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 4080kb

input:

5 5
1 4
2 3
1 3
2 5
5 1

output:

1 1 1 2 1 

result:

ok AC

Test #2:

score: 0
Accepted
time: 1ms
memory: 4080kb

input:

5 7
1 2
2 1
4 3
5 1
5 4
4 1
4 5

output:

1 1 2 1 1 

result:

ok AC

Test #3:

score: 0
Accepted
time: 1ms
memory: 3864kb

input:

8 6
6 1
3 4
3 6
2 3
4 1
6 4

output:

1 1 1 1 2 1 3 4 

result:

ok AC

Test #4:

score: -100
Wrong Answer
time: 491ms
memory: 5716kb

input:

7000 6999
4365 4296
2980 3141
6820 4995
4781 24
2416 5844
2940 2675
3293 2163
3853 5356
262 6706
1985 1497
5241 3803
353 1624
5838 4708
5452 3019
2029 6161
3849 4219
1095 1453
4268 4567
1184 1857
2911 3977
1662 2751
6353 6496
2002 6628
1407 4623
425 1331
4445 4277
1259 3165
4994 1044
2756 5788
5496 ...

output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

result:

wrong answer 5 and 1 are not connected