QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#331513#1844. Cactussinsop90WA 100ms80812kbC++143.2kb2024-02-18 12:34:052024-02-18 12:34:06

Judging History

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

  • [2024-02-18 12:34:06]
  • 评测
  • 测评结果:WA
  • 用时:100ms
  • 内存:80812kb
  • [2024-02-18 12:34:05]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int n, m, dfn[maxn], low[maxn], S[maxn], top, cnt, head[maxn], tot, col, g[maxn][2], deg[maxn], vis[maxn], visn[maxn], vism[maxn];
queue<int> Q;
struct edge {
	int v, pre;
}e[maxn << 1];
void add(int u, int v) {
	e[++ tot].v = v;
	e[tot].pre = head[u];
	head[u] = tot;
}
vector<int> belong[maxn], vecn, vec[maxn];
vector<pair<int, int>> ans;
void check(int x, int y) {
	if(!g[x][0]) g[x][0] = y;
	else g[x][1] = y;
}
void tarjan(int now, int fa) {
	dfn[now] = low[now] = ++ cnt;
	S[++ top] = now;
	for(int i = head[now];i;i = e[i].pre) {
		int v = e[i].v;
		if(vis[v]) continue;
		if(!dfn[v]) {
			tarjan(v, now);
			low[now] = min(low[now], low[v]);
			if(low[v] >= dfn[now]) {
				++ col;
				belong[col].push_back(now);
				check(now, col);
				while(1) {
					int t = S[top];
					check(t, col);
					belong[col].push_back(t);
					top --;
					if(t == v) break;
				}
			}
		}
		else if(v != fa) low[now] = min(low[now], dfn[v]);
	}
}
void dfs(int now, int ID, int fa) {
	vecn.push_back(now);
	vism[now] = 1;
	for(int i = head[now];i;i = e[i].pre) {
		int v = e[i].v;
		if(v == fa || vism[v]) continue;
		if(g[v][0] != ID && g[v][1] != ID) continue;
		dfs(v, ID, now);
	}
}
int main() {
//	freopen("a.in", "r", stdin);
//	freopen("my.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n >> m;
	for(int i = 1, u, v;i <= m;i++) {
		cin >> u >> v;
		add(u, v), add(v, u);
		deg[u] ++, deg[v] ++;
	}
	for(int i = 1;i <= n;i++) if(deg[i] & 1) Q.push(i);
	while(!Q.empty()) {
		int t = Q.front();
		Q.pop();
		if(vis[t]) continue;
		if(deg[t] % 2 == 0) continue;
		ans.push_back(make_pair(1, t));
		vis[t] = 1;
		for(int i = head[t];i;i = e[i].pre) {
			int v = e[i].v;
			if(vis[v]) continue;
			deg[v] --;
			if(deg[v] & 1) Q.push(v);
		}
	}
//	return 0;
	ans.push_back(make_pair(2, 0));
	for(int i = 1;i <= n;i++) {
		deg[i] = 0;
		if(vis[i]) continue;
		if(!dfn[i]) tarjan(i, 0);
	}
	for(int i = 1;i <= n;i++) {
		if(vis[i]) continue;
		if(g[i][1]) vec[g[i][0]].push_back(g[i][1]), vec[g[i][1]].push_back(g[i][0]), deg[g[i][0]] ++, deg[g[i][1]] ++;
	}
	for(int i = 1;i <= col;i++) if(deg[i] == 0) Q.push(i);
	while(!Q.empty()) {
		int t = Q.front(), rt = 0;
		visn[t] = 1;
		vecn.clear();
		Q.pop();
		for(int x : belong[t]) {
			if(!rt) rt = x;
			if(!g[x][1]) continue;
			int P = t ^ g[x][0] ^ g[x][1];
			if(!visn[P]) rt = x;
		}
		dfs(rt, t, rt);
		for(int x : belong[t]) vism[x] = 0;
		for(int i = 1;i < vecn.size();i++) {
			ans.push_back(make_pair(1, (i & 1) ? vecn[i] : vecn[i] + n));
		}
		ans.push_back(make_pair(1, vecn[1] + n));
		if(vecn.size() & 1) ans.push_back(make_pair(1, vecn.back()));
		else ans.push_back(make_pair(1, vecn.back() + n));
		for(int v : vec[t]) {
			deg[v] --;
			if(!deg[v]) Q.push(v);
		}
	}
	for(pair<int, int> t : ans) vism[t.second] = 1;
	for(int i = 1;i <= n;i++) if(!vism[i] || vis[i]) ans.push_back(make_pair(1, i));
	cout << 0 << " " << ans.size() << '\n';
	for(pair<int, int> t : ans) {
		if(t.first == 2) cout << 2 << '\n';
		else cout << t.first << " " << t.second << '\n';
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 7ms
memory: 60064kb

input:

3 3
1 2
1 3
2 3

output:

0 6
2
1 3
1 5
1 6
1 2
1 1

result:

ok You are right!

Test #2:

score: 0
Accepted
time: 11ms
memory: 60388kb

input:

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

output:

0 14
1 4
1 5
1 6
1 7
2
1 3
1 9
1 10
1 2
1 1
1 4
1 5
1 6
1 7

result:

ok You are right!

Test #3:

score: -100
Wrong Answer
time: 100ms
memory: 80812kb

input:

300000 368742
1 143504
1 234282
2 91276
2 296320
3 274816
4 212293
4 258214
5 253489
5 295826
6 96521
6 252745
6 267103
6 269879
7 5293
7 295586
8 44304
8 57067
8 233291
9 190526
10 18682
11 7440
12 24695
12 172561
12 243692
12 280316
13 80152
13 268749
14 146394
14 207280
15 151280
15 226848
16 458...

output:

0 510530
1 3
1 8
1 9
1 10
1 11
1 16
1 20
1 21
1 25
1 28
1 29
1 30
1 32
1 33
1 34
1 41
1 42
1 43
1 44
1 47
1 48
1 53
1 54
1 55
1 57
1 59
1 62
1 65
1 73
1 75
1 77
1 80
1 81
1 87
1 88
1 94
1 95
1 101
1 102
1 103
1 106
1 112
1 123
1 128
1 129
1 133
1 136
1 137
1 138
1 140
1 141
1 143
1 145
1 146
1 150
1...

result:

wrong answer The deg of 488 is not odd.