답안 #782633

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
782633 2023-07-14T06:41:18 Z makanhulia Naboj (COCI22_naboj) C++17
110 / 110
245 ms 18056 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAXN = 2e5 + 5;
const ll MAXM = 5e5 + 5;

ll n, m;
vector<ll> adjl[MAXN], ans;
bool visited[MAXN], visited2[MAXN], recur[MAXN], flag = false;
set<ll> st;

void dfs(ll now) {
  if (visited[now])
  {
    flag = true;
    return ;
  }
  visited[now] = true;

  vector<ll> adjn = adjl[now];
  for (int i = 0; i < adjn.size(); ++i)
  {
    ll nxt = adjn[i];
    if (visited[nxt]) {
      continue ;
    }

    dfs(nxt);

    if (flag) {
      return ;
    }
  }

  ans.push_back(now);
}

void dfs2(ll now) {
  if (recur[now]) {
    flag = true;
    return ;
  }

  if (visited2[now])
  {
    return ;
  }
  visited2[now] = true;
  recur[now] = true;

  vector<ll> adjn = adjl[now];
  for (int i = 0; i < adjn.size(); ++i)
  {
    ll nxt = adjn[i];
    if (recur[nxt]) {
      flag = true;
      return ;
    }
    if (visited2[nxt]) {
      continue ;
    }

    dfs2(nxt);
    if (flag) {
      return ;
    }
  }

  recur[now] = false;
}

int main(){
  ios_base::sync_with_stdio(false); cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; ++i)
  {
    ll tmpa, tmpb;
    cin >> tmpa >> tmpb;
    adjl[tmpb].push_back(tmpa);
  }

  // Cek cycle
  for (int i = 1; i <= n; ++i)
  {
    if (visited2[i]) {
      continue ;
    }

    dfs2(i);

    if (flag) {
      cout << "-1\n";
      return 0;
    }  
  }

  if (flag) {
    cout << "-1\n";
    return 0;
  }  

  for (int i = 1; i <= n; ++i)
  {
    if (visited[i]) {
      continue ;
    }

    dfs(i);
  }

  cout << ans.size() << "\n";
  for (int i = ans.size()-1; i >= 0 ; --i)
  {
    cout << ans[i] << " 1\n";
  }
  return 0;
}

Compilation message

naboj.cpp: In function 'void dfs(ll)':
naboj.cpp:21:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |   for (int i = 0; i < adjn.size(); ++i)
      |                   ~~^~~~~~~~~~~~~
naboj.cpp: In function 'void dfs2(ll)':
naboj.cpp:52:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |   for (int i = 0; i < adjn.size(); ++i)
      |                   ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 4 ms 5028 KB Output is correct
3 Correct 4 ms 5028 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 4 ms 5028 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 5024 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 4 ms 5028 KB Output is correct
12 Correct 4 ms 4948 KB Output is correct
13 Correct 4 ms 4948 KB Output is correct
14 Correct 3 ms 5032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 134 ms 14716 KB Output is correct
2 Correct 116 ms 13900 KB Output is correct
3 Correct 64 ms 9992 KB Output is correct
4 Correct 156 ms 13928 KB Output is correct
5 Correct 126 ms 13880 KB Output is correct
6 Correct 132 ms 13956 KB Output is correct
7 Correct 123 ms 13884 KB Output is correct
8 Correct 89 ms 12076 KB Output is correct
9 Correct 104 ms 13892 KB Output is correct
10 Correct 131 ms 13868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 4 ms 5028 KB Output is correct
3 Correct 4 ms 5028 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 4 ms 5028 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 5024 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 4 ms 5028 KB Output is correct
12 Correct 4 ms 4948 KB Output is correct
13 Correct 4 ms 4948 KB Output is correct
14 Correct 3 ms 5032 KB Output is correct
15 Correct 134 ms 14716 KB Output is correct
16 Correct 116 ms 13900 KB Output is correct
17 Correct 64 ms 9992 KB Output is correct
18 Correct 156 ms 13928 KB Output is correct
19 Correct 126 ms 13880 KB Output is correct
20 Correct 132 ms 13956 KB Output is correct
21 Correct 123 ms 13884 KB Output is correct
22 Correct 89 ms 12076 KB Output is correct
23 Correct 104 ms 13892 KB Output is correct
24 Correct 131 ms 13868 KB Output is correct
25 Correct 120 ms 12612 KB Output is correct
26 Correct 110 ms 11884 KB Output is correct
27 Correct 134 ms 12836 KB Output is correct
28 Correct 194 ms 14200 KB Output is correct
29 Correct 91 ms 10924 KB Output is correct
30 Correct 176 ms 14056 KB Output is correct
31 Correct 24 ms 6644 KB Output is correct
32 Correct 181 ms 15740 KB Output is correct
33 Correct 245 ms 17652 KB Output is correct
34 Correct 95 ms 15776 KB Output is correct
35 Correct 176 ms 17720 KB Output is correct
36 Correct 89 ms 15716 KB Output is correct
37 Correct 143 ms 17144 KB Output is correct
38 Correct 198 ms 16620 KB Output is correct
39 Correct 194 ms 17524 KB Output is correct
40 Correct 227 ms 16916 KB Output is correct
41 Correct 137 ms 16824 KB Output is correct
42 Correct 214 ms 17932 KB Output is correct
43 Correct 107 ms 15924 KB Output is correct
44 Correct 190 ms 18056 KB Output is correct
45 Correct 105 ms 15848 KB Output is correct
46 Correct 183 ms 16836 KB Output is correct