QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#151639#6329. Colorful GraphForever_Young#WA 486ms5496kbC++143.5kb2023-08-27 12:13:412023-08-27 12:13:45

Judging History

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

  • [2023-08-27 12:13:45]
  • 评测
  • 测评结果:WA
  • 用时:486ms
  • 内存:5496kb
  • [2023-08-27 12:13:41]
  • 提交

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],nxt[N],du[N];
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,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=belong[save[i].first];
		int y=belong[save[i].second];
		nxt[x]=y; du[y]++;
	}
	int cur=0;
	rep(i,cnt)
	if (!du[i])
	{
		++cur;
		int p=i;
		while (p!=0)col[p]=cur,p=nxt[p];
	}
	rep(i,n)printf("%d ",col[belong[i]]);
	puts("");

	return 0;
}

詳細信息

Test #1:

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

input:

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

output:

2 2 2 1 2 

result:

ok AC

Test #2:

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

input:

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

output:

2 2 1 2 2 

result:

ok AC

Test #3:

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

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: 486ms
memory: 5496kb

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 0 0 0 1746 2 3 0 0 0 1750 1750 4 1750 0 5 0 6 0 0 0 0 1747 0 0 7 8 1730 0 9 0 0 10 11 0 0 0 12 1747 13 0 0 14 0 15 0 16 1747 17 18 0 19 0 1746 0 0 1747 20 0 21 0 1746 1746 1747 0 22 1747 1747 0 23 24 0 25 0 26 0 1730 0 0 1747 1747 1747 0 0 1750 0 0 0 0 1747 0 1746 1730 0 1747 0 0 0 27 0 1747 1746 ...

result:

wrong answer Integer 0 violates the range [1, 1750]