#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less_equal<>,
rb_tree_tag, tree_order_statistics_node_update>
ordered_set;
#define ll long long
#define ii pair<ll,ll>
#define endl '\n'
const ll inf=1e15;
const ll maxn=2e5+5;
const ll mod=1e9+7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll cnt[maxn][2];
bool vis[maxn];
set<ii> adj[maxn];
queue<ii> q;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n,m,a,b;
cin>>n>>m;
for (int i=0;i<m;i++){
cin>>a>>b;
adj[a].emplace(b,0);
adj[b].emplace(a,1);
cnt[a][0]++;
cnt[b][1]++;
}
for (int i=1;i<=n;i++){
if (cnt[i][0]==0){
q.emplace(i,0);
} else if (cnt[i][1]==0){
q.emplace(i,1);
}
}
stack<ii> s;
while (q.size()){
tie(a,b)=q.front();
q.pop();
for (auto [x,y]:adj[a]){
adj[x].erase(make_pair(a,b));
if (cnt[x][b]==1 && vis[x]==0){
q.emplace(x,y);
vis[x]=1;
}
cnt[x][b]--;
}
s.emplace(a,b);
}
for (int i=1;i<=n;i++){
if (cnt[i][0] && cnt[i][1]){
cout<<-1;
return 0;
}
}
cout<<s.size()<<endl;
while (s.size()){
cout<<s.top().first<<' '<<s.top().second<<endl;
s.pop();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
6 ms |
9724 KB |
Output is correct |
6 |
Correct |
5 ms |
9724 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
268 ms |
44176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
6 ms |
9724 KB |
Output is correct |
6 |
Correct |
5 ms |
9724 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |