Submission #782535

#TimeUsernameProblemLanguageResultExecution timeMemory
782535gun_ganNaboj (COCI22_naboj)C++17
0 / 110
64 ms13168 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MX = 2e5 + 5;

int N, M;
int deg[MX];
vector<int> g[MX];

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

      cin >> N >> M;
      for(int i = 0; i < M; i++) {
            int u, v;
            cin >> u >> v;
            g[v].push_back(u);
            deg[u]++;
      }

      queue<int> q;
      vector<int> ord;
      for(int i = 1; i <= N; i++) {
            if(!deg[i]) {
                  if(g[i].size()) ord.push_back(i);
                  q.push(i);
            }
      }

      int cnt = 0;
      while(!q.empty()) {
            auto v = q.front(); q.pop();
            cnt++;
            for(auto u : g[v]) {
                  deg[u]--;
                  if(!deg[u]) {
                        if(g[u].size()) ord.push_back(u);
                        q.push(u);
                  }
            }
      }

      if(cnt < N) {
            cout << -1 << '\n';
            return 0;
      }

      cout << ord.size() << '\n';
      for(auto x : ord) {
            cout << x << " " << 0 << '\n';
      }
}     
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...