답안 #781768

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
781768 2023-07-13T10:44:32 Z devariaota Naboj (COCI22_naboj) C++17
110 / 110
378 ms 50628 KB
#include<bits/stdc++.h>
#define int long long
#define pii pair<int, int>
using namespace std;
const int MAX=1e18;
signed main() {
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int n, m; cin >> n >> m;
  vector<vector<tuple<int, int, int>>> adj(n);
  stack<pair<int, int>> dfs;
  deque<pair<int, int>> ans;
  vector<int> here(n), away(n);
  vector<bool> used(m), vis(n);
  for(int i=0; i<m; i++) {
    int u, v; cin >> u >> v; --u; --v;
    adj[u].push_back({v, 0, i});
    adj[v].push_back({u, 1, i});
    here[u]++; away[v]++;
  }
  for(int i=0; i<n; i++) {
    if(here[i]==0 && away[i]==0) continue;
    if(here[i]==0) dfs.push({i, 0});
    if(away[i]==0) dfs.push({i, 1});
  }
  while(!dfs.empty()) {
    int u=dfs.top().first, type=dfs.top().second;
    dfs.pop(); vis[u]=1; 
    ans.push_front({u+1, type});
    if(type) {
      for(auto p : adj[u]) {
        used[get<2>(p)]=1;
        int pp=get<0>(p);
        if(vis[pp]) continue;
        away[pp]--;
        if(away[pp]==0 && here[pp]==0) continue;
        if(away[pp]==0) dfs.push({pp, 1}), used[get<2>(p)]=1;
      }
    } else {
      for(auto p : adj[u]) {
        used[get<2>(p)]=1;
        int pp=get<0>(p);
        if(vis[pp]) continue;
        here[pp]--;
        if(here[pp]==0 && away[pp]==0) continue;
        if(here[pp]==0) dfs.push({pp, 0}), used[get<2>(p)]=1;
      }
    }
  }
  for(int i=0; i<m; i++) {
    if(used[i]) continue;
    cout << "-1\n"; return 0;
  }
  cout << ans.size() << '\n';
  for(auto p : ans)
    cout << p.first << ' ' << p.second << '\n';
    return 0;
}

Compilation message

naboj.cpp: In function 'int main()':
naboj.cpp:54:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   54 |   for(auto p : ans)
      |   ^~~
naboj.cpp:56:5: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   56 |     return 0;
      |     ^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 133 ms 26192 KB Output is correct
2 Correct 135 ms 27784 KB Output is correct
3 Correct 61 ms 14752 KB Output is correct
4 Correct 152 ms 27704 KB Output is correct
5 Correct 139 ms 27736 KB Output is correct
6 Correct 164 ms 27820 KB Output is correct
7 Correct 133 ms 27808 KB Output is correct
8 Correct 119 ms 21764 KB Output is correct
9 Correct 142 ms 27788 KB Output is correct
10 Correct 132 ms 27760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 133 ms 26192 KB Output is correct
16 Correct 135 ms 27784 KB Output is correct
17 Correct 61 ms 14752 KB Output is correct
18 Correct 152 ms 27704 KB Output is correct
19 Correct 139 ms 27736 KB Output is correct
20 Correct 164 ms 27820 KB Output is correct
21 Correct 133 ms 27808 KB Output is correct
22 Correct 119 ms 21764 KB Output is correct
23 Correct 142 ms 27788 KB Output is correct
24 Correct 132 ms 27760 KB Output is correct
25 Correct 172 ms 37292 KB Output is correct
26 Correct 179 ms 33076 KB Output is correct
27 Correct 217 ms 37684 KB Output is correct
28 Correct 269 ms 45256 KB Output is correct
29 Correct 143 ms 29596 KB Output is correct
30 Correct 340 ms 44596 KB Output is correct
31 Correct 22 ms 8268 KB Output is correct
32 Correct 153 ms 29788 KB Output is correct
33 Correct 317 ms 47728 KB Output is correct
34 Correct 198 ms 31312 KB Output is correct
35 Correct 306 ms 48044 KB Output is correct
36 Correct 166 ms 29172 KB Output is correct
37 Correct 276 ms 43972 KB Output is correct
38 Correct 274 ms 38832 KB Output is correct
39 Correct 325 ms 47156 KB Output is correct
40 Correct 316 ms 41748 KB Output is correct
41 Correct 278 ms 41344 KB Output is correct
42 Correct 346 ms 49912 KB Output is correct
43 Correct 199 ms 33104 KB Output is correct
44 Correct 378 ms 50628 KB Output is correct
45 Correct 208 ms 31080 KB Output is correct
46 Correct 332 ms 41788 KB Output is correct