This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define int long long
#define forn(i,n) for(int i = 0;i < n;i++)
#define Forn(i,n) for(int i = 1;i <= n;i++)
#define vi vector<int>
#define pb push_back
#define pii pair<int,int>
#define all(p) p.begin(),p.end()
#define F first
#define S second
using namespace std;
void debug() {cerr << endl;}
template <class T, class ...U> void debug(T a, U ... b) { cerr << a << " "; debug(b...);}
const int INF = 1e13 + 10;
const int MOD = (int) 998244353;
const int N = 2e5 + 9;
int n,m;
vi e[N];
int in[N];
vi ord;
bool tag[N],vis[N];
void dfs(int u){
debug(u);
vis[u] = 1,tag[u] = 1;
for(int c : e[u]){
if(tag[c]){
cout << "-1\n";
exit(0);
}
if(!vis[c]){
dfs(c);
}
}
tag[u] = 0;
ord.pb(u);
}
bool st[N];
void solve(){
//pancake ( ^-^)_
cin >> n >> m;
forn(i,m){
int s,t;
cin >> s >> t;
e[s].pb(t);
in[t]++;
}
Forn(i,n){
if(!vis[i] && in[i] == 0) dfs(i),st[i] = 1;
}
if(ord.size() != n) {
cout << "-1\n";
return;
}
cout << ord.size() << '\n';
reverse(all(ord));
for(auto &i : ord) {
if(st[i]) cout << i << ' ' << 1 << '\n';
else cout << i << ' ' << 0 << '\n';
}
}
signed main(){
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(0);
solve();
return 0;
}
Compilation message (stderr)
naboj.cpp: In function 'void solve()':
naboj.cpp:54:16: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
54 | if(ord.size() != n) {
| ~~~~~~~~~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |