# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1137910 | jackofall718 | Naboj (COCI22_naboj) | C++20 | 0 ms | 0 KiB |
//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]<<" "<< 0 <<endl;//bracket around the check part is imp
check ^=1;
}
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
//cout << "Time taken by function: " << duration.count() << " microseconds" << endl;
return 0;
}