제출 #932903

#제출 시각아이디문제언어결과실행 시간메모리
932903a_l_i_r_e_z_aNaboj (COCI22_naboj)C++17
110 / 110
162 ms21252 KiB
// In the name of God
// Hope is last to die

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pb push_back
// #define int long long
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())

const int maxn = 2e5 + 5;
const int inf = 1e9 + 7;
int n, m, mark[maxn];
vector<int> adj[maxn], topol;

void dfs(int v){
    mark[v] = 1;
    for(auto u: adj[v]){
        if(!mark[u]) dfs(u);
        else if(mark[u] == 1){
            cout << -1 << '\n';
            exit(0);
        }
    }
    topol.pb(v);
    mark[v] = 2;
}

void topolo(){
    for(int i = 1; i <= n; i++) if(!mark[i]) dfs(i);
    reverse(all(topol));
}

int32_t main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n >> m;
    for(int i = 0; i < m; i++){
        int u, v; cin >> u >> v;
        adj[v].pb(u);
    }
    topolo();
    cout << n << '\n';
    for(int i = 0; i < n; i++){
        cout << topol[i] << ' ' << 1 << '\n';
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...