QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#115318#6329. Colorful Graphxaphoenix#RE 1ms3764kbC++143.8kb2023-06-25 17:05:022023-06-25 17:05:04

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-25 17:05:04]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3764kb
  • [2023-06-25 17:05:02]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pf push_front
#define LC k<<1
#define RC k<<1|1
#define IO cin.sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define rep(i,a,n) for (int i = a; i < n; i++)
#define repn(i,a,n) for (int i = a; i <= n; i++)
#define per(i,a,n) for (int i = (n) - 1; i >= a; i--)
#define pern(i,a,n) for (int i = n; i >= a; i--)

typedef long long LL;
typedef long double LD;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
typedef pair<double, double> PDD;
typedef pair<ull, ull> PUU;
typedef pair<LL, LL> PLL;

const int N = 7100;
const int M = 110000;
const int mod = 1e9+7;
const int inf = (int)1e9;
const LL INF = 1e18;
const double eps = 1e-9;

mt19937_64 Rand((unsigned long long)new char);
#define rand Rand

int n, m;
int dfn[N], low[N], instack[N], scc[N], cnt, scc_cnt;
stack<int> st;
vector<int> g[N];
void tarjan(int x) {
	dfn[x] = low[x] = ++cnt;
	st.push(x);
	instack[x] = 1;
	for (auto y: g[x]) {
		if (dfn[y] == 0) {
			tarjan(y);
			low[x] = min(low[x], low[y]);
		}
		else if (instack[y]) low[x] = min(low[x], dfn[y]);
	}
	if (low[x]==dfn[x]) {
		++scc_cnt;
		while (!st.empty() && st.top() != x) {
			scc[st.top()] = scc_cnt;
			instack[st.top()] = 0;
			st.pop();
		}
		st.pop();
		scc[x] = scc_cnt;
		instack[x] = 0;
	}
}

int fa[N], col[N];
int ans[N], que[N], tail, tot;
int find(int x) {
	return fa[x] == x? x: fa[x] = find(fa[x]);
}
struct MCMF {
	// bfs, dinic
	// st must be the lowest index, ed must be the highest index
	int e[M], f[M], pre[M], last[N];
	int d[N], now[N];
	int num, st, ed, maxflow;
	queue<int> q;
	int left, right;
	void init() {
		num = 1;
		repn(i, st, ed) last[i] = 0;
		maxflow = 0;
	}
	void insert(int x, int y, int z) {
		e[++num] = y, f[num] = z, pre[num] = last[x], last[x] = num;
		e[++num] = x, f[num] = 0, pre[num] = last[y], last[y] = num;
	}
	bool bfs() {
	    memset(d, -1, sizeof(d));
	    d[st] = 0;
		q.push(st);
	    while(!q.empty()) {
	        int now = q.front();
	        for (int i = last[now]; i; i = pre[i]) if (f[i] && d[e[i]] == -1) {
	                d[e[i]] = d[now] + 1;
	                q.push(e[i]); 
	            }
	        q.pop();
	    }
	    if (d[ed] == -1) return 0;
		return 1;
	}
	int dfs(int x, int incf) {
	    if (x == ed) return incf;
	    int flow = 0, w;
	    for (int i = now[x]; i; i = pre[i]) if (f[i] && d[e[i]] == d[x] + 1) {
				w = dfs(e[i], min(incf - flow, f[i]));
	            f[i] -= w, f[i ^ 1] += w; flow += w;
	            if (f[i]) now[x] = i;
				if (flow == incf) {
					if (x == st) left = (e[i] + 1) / 2;
					if (e[i] == ed) right = (x + 1) / 2;
					return incf;
				}
	        }
	    if (!flow) d[x] = -1;
	    return flow;
	}
	void run() {
		while(bfs()) {
			repn(i, st, ed) now[i] = last[i];
			maxflow += dfs(st, 1);
			int fx = find(left), fy = find(right);
			fa[fx] = fy;
		}
	}
		
}solver;
int main() {
	IO;
	cin >> n >> m;
	repn(i, 1, m) {
		int x, y;
		cin >> x >> y;
		g[x].pb(y);
	}
	repn(i, 1, n) if (dfn[i] == 0) tarjan(i);
	solver.st = 0, solver.ed = scc_cnt * 2 + 1;
	solver.init();
	repn(i, 1, scc_cnt) {
		solver.insert(0, i * 2 - 1, 1), solver.insert(i * 2, scc_cnt * 2 + 1, 1);
		solver.insert(i * 2, i * 2 - 1, n);
	}
	repn(x, 1, n) for (auto y: g[x]) {
		if (scc[x] == scc[y]) continue;
		solver.insert(2 * scc[x] - 1, 2 * scc[y], n);
	}
	repn(i, 1, scc_cnt) fa[i] = i;
	solver.run();
	repn(i, 1, scc_cnt) if (!ans[find(i)]) ans[find(i)] = ++tot;
	repn(i, 1, n) cout << ans[find(scc[i])] << " \n"[i == n];
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3728kb

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: 3764kb

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
Runtime Error

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:


result: