QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#528890#1824. Special CyclePetroTarnavskyiWA 88ms3652kbC++235.2kb2024-08-24 00:33:172024-08-24 00:33:17

Judging History

This is the latest submission verdict.

  • [2024-08-24 00:33:17]
  • Judged
  • Verdict: WA
  • Time: 88ms
  • Memory: 3652kb
  • [2024-08-24 00:33:17]
  • Submitted

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 vector<LL> VL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef double db;

const int N = 1 << 10; 

vector<PII> G[N];
VI ord;
int us[N];
int n;
int bads = 0;
bool dfs(int v, int id, int it)
{
	us[v] = 1;
	if(v >= 2 * n && us[v ^ 1] == 1)
		bads++;
	ord.PB(v);
	for(auto [to, ind] : G[v])
	{
		if(ind == id || us[to] == 2)
			continue;
		if(us[to] == 0)
		{
			if(dfs(to, ind, it))
				return true;
			continue;
		}
		if(it == 0 && bads != 0)
			continue;
			
		ord.PB(to);
		return true;
	}
	ord.pop_back();
	us[v] = 2;
	if(v >= 2 * n && us[v ^ 1] == 1)
		bads--;
	return false;
}


VI findCycle(int sz, int v, vector<tuple<int, int, int>> edges, int it)
{
	ord.clear();
	fill(us, us + N, 0);
	bads = 0;
	FOR(i, 0, N)
		G[i].clear();
	for(auto [a, b, i] : edges)
		G[a].PB(MP(b, i));
	if(!dfs(v, -1, it))
		return {};
	
	v = ord.back();
	ord.pop_back();
	VI ans;
	while(ord.back() != v)
	{
		ans.PB(ord.back());
		ord.pop_back();
	}
	ans.PB(v);
	return ans;
}

void print(VI ans)
{
	cout << SZ(ans) << "\n";
	for(int v : ans)
		cout << v + 1 << "\n";
	exit(0);
}


void check(vector<PII> badEdges, vector<tuple<int, int, int>> edges, VI ans)
{
	if(SZ(ans) < 3)
		return;
	set<PII> eg;
	for(auto [a, b] : badEdges)
	{
		eg.insert(MP(a, b));
		eg.insert(MP(b, a));
	}
	for(auto [a, b, i] : edges)
		eg.insert(MP(a, b));
	
	
	VI ok(n), pos(n);
	FOR(i, 0, SZ(ans))
	{
		int v = ans[i];
		pos[v] = i;
		if(ok[v])
			return;
		ok[v] = 1;
		int to = ans[(i + 1) % SZ(ans)];
		if(!eg.count(MP(v, to)))
			return;
	}
	for(auto [a, b] : badEdges)
	{
		if(ok[a] + ok[b] == 1)
			return;
		if(pos[a] > pos[b])
			swap(a, b);
		if(pos[a] + 1 == pos[b])
			return;
		if(pos[a] == 0 && pos[b] == SZ(ans) - 1)
			return;
	}		
	print(ans);
}



VI g[N];
VI order;
bool used[N];
void dfs(int v)
{
	used[v] = 1;
	order.PB(v);
	for(int to : g[v])
		if(!used[to])
			dfs(to);
}


mt19937 rng(74);
int main()
{
	ios::sync_with_stdio(0); 
	cin.tie(0);
	
	int m, k;
	cin >> n >> m >> k;
	
	vector<PII> badEdges(k);
	vector<tuple<int, int, int>> edges(2 * (m - k));
	VI badV(n);
	FOR(i, 0, k)
	{
		int a, b;
		cin >> a >> b;
		a--; b--;
		badV[a]++;
		badV[b]++;
		badEdges[i] = MP(a, b);
	}
	FOR(i, 0, m - k)
	{
		int a, b;
		cin >> a >> b;
		a--; b--;
		edges[2 * i] = {a, b, i};
		edges[2 * i + 1] = {b, a, i};
	}
	
	
	for(auto [a, b] : badEdges)
	{
		g[a].PB(b);
		g[b].PB(a);
	}
	map<PII, VI> orders;
	FOR(it, 0, 2)
	FOR(i, 0, n)
	{
		if(used[i] || badV[i] == 0)
			continue;
		if(it == 0 && badV[i] == 2)
			continue;
		order.clear();
		dfs(i);
		int mx = 0, mn = N;
		for(int v : order)
		{
			mx = max(mx, badV[v]);
			mn = min(mn, badV[v]);
		}
		if(mx == 2 && mn == 2)
			print(order);
		if(mx > 2)
		{
			for(int v : order)
				badV[v] = 47;
			continue;
		}
		int to = order.back();
		orders[MP(i, to)] = order;
		reverse(ALL(order));
		orders[MP(to, i)] = order;		
		
	}
	
	vector<tuple<int, int, int>> nedg;
	for(auto [a, b, i] : edges)
	{
		if(badV[a] != 0 || badV[b] != 0)
			continue;
		nedg.PB({a, b, i});
	}
	
	FOR(v, 0, n)
	{
		VI ans = findCycle(n, v, nedg, 0);
		check(badEdges, edges, ans);
	}
	
	
	vector<tuple<int, int, int>> edg;
	for(auto [pii, _] : orders)
	{
		auto [a, b] = pii;
		int A = 2 * n + 2 * a;
		int B = 2 * n + 2 * b;
		
		edg.PB({A, B ^ 1, SZ(edg)});
		edg.PB({B, A ^ 1, SZ(edg)});
	}
	for(auto [a, b, _] : edges)
	{
		if(a > b)
			continue;
		if(badV[a] == 0 && badV[b] == 0)
		{
			int sz = SZ(edg);
			edg.PB({a, b, sz});
			edg.PB({b, a, sz});
			continue;
		}
		if(badV[a] > 1 || badV[b] > 1)
			continue;
		if(badV[a] == 1 && badV[b] == 1)
		{
			int A = 2 * n + 2 * a;
			int B = 2 * n + 2 * b;
		
			edg.PB({A ^ 1, B, SZ(edg)});
			edg.PB({B ^ 1, A, SZ(edg)});
			continue;
		}
		if(badV[a] == 0)
			swap(a, b);
		int A = 2 * n + 2 * a;
		
		int sz = SZ(edg);
		edg.PB({A ^ 1, b, sz});
		edg.PB({b, A, sz});	
	}
	VI tot(n);
	iota(ALL(tot), 0);
	check(badEdges, edges, tot);
	
	FOR(it, 0, 3000)
	{
		FOR(st, 0, 3 * n)
		{
			VI res = findCycle(3 * n, st, edg, it % 2);
			if(SZ(res) == 0)
				continue;
			for(int& v : res)
				if(v >= 2 * n)
					v = (v - 2 * n) / 2;
					
			VI ans = {res[0]};
			FOR(i, 1, SZ(res))
			{
				int v = ans.back();
				int to = res[i];
				if(orders.count(MP(v, to)))
				{
					ans.pop_back();
					VI cur = orders[MP(v, to)];
					for(int x : cur)
						ans.PB(x);
					continue;
				}
				ans.PB(to);
			}
			int v = ans.back(), to = ans[0];
			if(SZ(res) != 2 && orders.count(MP(v, to)))
			{
				ans.pop_back();
				VI cur = orders[MP(v, to)];
				for(int x : cur)
					ans.PB(x);
				ans.pop_back();
			}
			
			check(badEdges, edges, ans);
		}
		if(it % 2 == 1)
			shuffle(ALL(edg), rng);
	}
	cout << "-1\n";

	return 0;
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 88ms
memory: 3652kb

input:

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

output:

-1

result:

wrong answer ! judge says solution, but team says no solution