제출 #936968

#제출 시각아이디문제언어결과실행 시간메모리
936968089487Naboj (COCI22_naboj)C++14
110 / 110
190 ms27264 KiB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse4,abm,avx,popcnt")
#include<bits/stdc++.h>
#define int long long
#define quick ios::sync_with_stdio(0);cin.tie(0);
#define rep(x,a,b) for(int x=a;x<=b;x++)
#define repd(x,a,b) for(int x=a;x>=b;x--)
#define F first
#define S second
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size())
#define lowbit(x) (x&-x)
using namespace std;
typedef pair<int,int> pii;
void debug(){
    cout<<"\n";
}
template<class T,class ...U>
void debug(T a,U ...b){
    cout<<a<<" ",debug(b...);
}
const int N=2e5+7;
const int INF=1e18L;
vector<int> v[N];
int out[N];
bool lgt[N];
signed main(){
    quick
    int n,m;
    cin>>n>>m;
    rep(i,1,m){
        int a,b;
        cin>>a>>b;
        v[b].eb(a);
        lgt[a]=true;
        out[a]++;
    }
    queue<int> q;
    vector<pii> ans;
    rep(i,1,n){
        if(!out[i]) q.push(i);
    }
    while(sz(q)){
        int x=q.front();
        q.pop();
        ans.eb(x,lgt[x]);
        for(int j:v[x]){
            out[j]--;
            if(!out[j]) q.push(j);
        }
    }
    if(sz(ans)!=n) cout<<"-1\n";
    else{
        cout<<sz(ans)<<"\n";
        for(pii p2:ans) cout<<p2.F<<" "<<p2.S<<"\n";
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...