제출 #852386

#제출 시각아이디문제언어결과실행 시간메모리
852386serifefedartarNaboj (COCI22_naboj)C++17
0 / 110
99 ms18004 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll LOGN = 20;
const ll MAXN = 2e5 + 5;

vector<vector<pair<int,int>>> graph;
int indeg[MAXN], outdeg[MAXN], vis[MAXN];
int main() {
	fast
	int n, m, a, b;
	cin >> n >> m;

	graph = vector<vector<pair<int,int>>>(n+1, vector<pair<int,int>>());
	for (int i = 0; i < m; i++) {
		cin >> a >> b;
		graph[a].push_back({b, 0});
		graph[b].push_back({a, 1});
		indeg[a]++;
		outdeg[b]++;
	}

	queue<int> q;
	for (int i = 1; i <= n; i++) {
		if (indeg[i] == 0 || outdeg[i] == 0)
			q.push(i);
	}

	stack<pair<int,int>> ans;
	while (!q.empty()) {
		int node = q.front();
		vis[node] = true;
		q.pop();

		int state = -1;
		for (auto u : graph[node]) {
			if (vis[u.f])
				continue;

			if (u.s == 1)
				indeg[u.f]--;
			else
				outdeg[u.f]--;

			if (indeg[u.f] == 0 || outdeg[u.f] == 0)
				q.push(u.f);
			state = u.s;
		}

		if (state != -1)
			ans.push({node, !state}); 
	}

	for (int i = 1; i <= n; i++) {
		if (!vis[i]) {
			cout << "-1\n";
			return 0;
		}
	}

	cout << ans.size() << "\n"; 
	while (!ans.empty()) {
		cout << ans.top().f << " " << ans.top().s << "\n";
		ans.pop();
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...