#include <bits/stdc++.h>
#include <chrono>
#define ll long long int
#define endl '\n'
#define vn vector<ll>
using namespace std;
using namespace std::chrono;
const int MAX_N = 1e9 + 7;
#define pii pair<ll,ll>
const ll INF = 0x3f3f3f3f3f3f3f3f;
#define pb push_back
#define srt(vp) sort(vp.begin(), vp.end())
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
auto start = high_resolution_clock::now();
ll n, m;
cin >> n >> m;
vector<vn> adj(n+1);
vn deg(n+1, 0); // degree vector to track the in-degrees
for (int i = 0; i < m; i++) {
ll u, v;
cin >> u >> v;
adj[v].pb(u); // reverse the direction to match the problem's requirements
deg[u]++;
}
priority_queue<int, vector<int>, greater<int>> noIndegree;
for (int i = 1; i <= n; i++) {
if (deg[i] == 0) noIndegree.push(i);
}
vn ans;
while (!noIndegree.empty()) {
ll u = noIndegree.top();
noIndegree.pop();
ans.pb(u);
for (auto v : adj[u]) {
deg[v]--;
if (deg[v] == 0) noIndegree.push(v);
}
}
if (ans.size() != n) {
cout << -1 << endl;
} else {
// Adjust this part to implement the actual electron flow directions based on problem constraints
cout << n << endl;
bool check = true; // Let's assume initially we charge positively
for (int i = 0; i < n; i++) {
cout << ans[i] << " " << (check ? 1 : 0) << endl;
check ^= 1; // Alternate the charge if necessary
}
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
// Uncomment below to see the duration in microseconds
// cout << "Time taken by function: " << duration.count() << " microseconds" << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |