//COCI 2021-22 ROUND 6 PROBLEM 3
#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);
for (int i=0;i<m;i++){
ll u,v;
cin>>u>>v;
adj[u].pb(v);
deg[v]++;
}
priority_queue<int, vector<int>, greater<int>> noIndegree;//so basically a min heap where elements are sorted in ascending order
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 {
cout<<n<<endl;
for (int i=0;i<n;i++){
cout<<ans[i]<<" "<< 1 <<endl;//initally I was alternating charges but thats wrong..if we follow the order of the topo sort simply charging them all with one crrent positive or negative works
}
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
//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... |