Submission #782633

#TimeUsernameProblemLanguageResultExecution timeMemory
782633makanhuliaNaboj (COCI22_naboj)C++17
110 / 110
245 ms18056 KiB
#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 (stderr)

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)
      |                   ~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...