QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#372160#3315. Eulerian Flight TourPetroTarnavskyiWA 0ms3556kbC++204.0kb2024-03-31 00:22:582024-03-31 00:22:59

Judging History

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

  • [2024-03-31 00:22:59]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3556kb
  • [2024-03-31 00:22:58]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

struct DSU
{
	int n;
	VI p, sz;
	
	void init(int _n)
	{
		n = _n;
		p.resize(n);
		iota(ALL(p), 0);
		sz.assign(n, 1);
	}
	
	int find(int v)
	{
		if (v == p[v])
			return v;
		return p[v] = find(p[v]);
	}
	
	bool unite(int u, int v)
	{
		u = find(u);
		v = find(v);
		if (u == v)
			return false;
		if (sz[u] > sz[v])
			swap(u, v);
		p[u] = v;
		sz[v] += sz[u];
		return true;
	}
} d;

const int N = 147;
int n;
int deg[N];
int e[N][N];
int g[N][N];
int ng[N][N];
bool used[N];

bool dfs(int v, int p = -1)
{
	used[v] = true;
	if (p != -1 && (deg[v] & 1))
	{
		return true;
	}
	FOR (i, 0, n)
	{
		if (!used[i] && ng[v][i])
		{
			bool ok = dfs(i, v);
			if (ok)
			{
				e[v][i] ^= 1;
				e[i][v] ^= 1;
				deg[v]++;
				deg[i]++;
				return true;
			}
		}
	}
	return false;
}

void solve()
{
	vector<VI> comps(n);
	FOR (i, 0, n)
	{
		comps[d.find(i)].PB(i);
		//cerr << i << ' ' << d.find(i) << '\n';
	}
	VI odd;
	FOR (i, 0, n)
	{
		auto c = comps[i];
		sort(ALL(c), [&](int u, int v)
		{
			return (deg[u] & 1) > (deg[v] & 1);
		});
		if (!c.empty() && deg[c[0]] & 1)
			odd.PB(i);
	}
	//cerr << "Odd " << SZ(odd) << '\n';
	vector<PII> ans;
	FOR (i, 0, SZ(odd))
	{
		int j = odd[i];
		int v = comps[j][0];
		int idx = odd[(i + 1) % SZ(odd)];
		int k = 1;
		while (k < SZ(comps[idx]) && deg[comps[idx][k]] & 1)
		{
			e[v][comps[idx][k]]++;
			e[comps[idx][k++]][v]++;
		}
		d.unite(comps[j][0], comps[idx][0]);
	}
	comps.clear();
	comps.resize(n);
	FOR (i, 0, n)
	{
		comps[d.find(i)].PB(i);
	}
	VI idx;
	FOR (i, 0, n)
	{
		if (comps[i].empty())
			continue;
		idx.PB(i);
	}
	if (SZ(idx) > 2)
	{
		FOR (i, 0, SZ(idx))
		{
			int j = (i + 1) % SZ(idx);
			e[comps[idx[i]][0]][comps[idx[j]][0]]++;
			e[comps[idx[j]][0]][comps[idx[i]][0]]++;
		}
	}
	else if (SZ(idx) == 2)
	{
		if (SZ(comps[idx[0]]) >= 2 && SZ(comps[idx[1]]) >= 2) 
		{
			FOR (i, 0, 2)
			{
				FOR (j, 0, 2)
				{
					int u = comps[idx[0]][i];
					int v = comps[idx[1]][j];
					e[u][v]++;
					e[v][u]++;
				}
			}
		}
		else
		{
			if (SZ(comps[idx[0]]) != 1)
				swap(idx[0], idx[1]);
			int u = comps[idx[0]][0];
			int v = -1;
			int w = -1;
			FOR (i, 0, SZ(comps[idx[1]]))
			{
				FOR (j, 0, i)
				{
					int x = comps[idx[1]][i];
					int y = comps[idx[1]][j];
					if (ng[x][y])
						v = x, w = y;
				}
			}
			if (v == -1)
			{
				cout << -1 << '\n';
				return;
			}
			e[u][v] ^= 1;
			e[v][u] ^= 1;
			e[u][w] ^= 1;
			e[v][w] ^= 1;
			e[w][u] ^= 1;
			e[w][v] ^= 1;
		}
	}
	FOR (i, 0, n)
	{
		FOR (j, 0, i)
		{
		//	cerr << i << ' ' << j << ' ' << e[i][j] << '\n';
			if (e[i][j] & 1)
			{
				ans.PB({j, i});
			}
		}
	}
	cout << SZ(ans) << '\n';
	for (auto [u, v] : ans)
		cout << u + 1 << ' ' << v + 1 << '\n';
	
	
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int m;
	cin >> n >> m;
	d.init(n);
	FOR (i, 0, m)
	{
		int u, v;
		cin >> u >> v;
		u--, v--;
		d.unite(u, v);
		g[u][v] = 1;
		g[v][u] = 1;
		deg[u]++;
		deg[v]++;
	}
	if (d.sz[d.find(0)] != n)
	{
		solve();
		return 0;
	}
	FOR (i, 0, n) FOR (j, 0, n)
	{
		if (i == j)
			continue;
		ng[i][j] = 1 - g[i][j];
	}
	FOR (i, 0, n)
	{
		if (deg[i] & 1)
		{
			if (!dfs(i))
			{
				cout << -1 << '\n';
				return 0;
			}
			fill(used, used + n, false);
		}
	}
	vector<PII> ans;
	FOR (i, 0, n)
	{
		FOR (j, 0, i)
			if (e[i][j] & 1)
				ans.PB({j, i});
	}
	cout << SZ(ans) << '\n';
	for (auto [u, v] : ans)
		cout << u + 1 << ' ' << v + 1 << '\n';
	
	
	return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3556kb

input:

11 10
1 2
2 3
3 4
4 5
6 7
7 8
8 9
9 10
10 11
6 11

output:

4
1 6
2 6
1 7
2 7

result:

wrong answer WRONG ANSWER
Non-even degree node: 1 d=3