Submission #836762

#TimeUsernameProblemLanguageResultExecution timeMemory
836762becaidoThousands Islands (IOI22_islands)C++17
100 / 100
262 ms50092 KiB
#pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx,popcnt,sse4,abm") #include <bits/stdc++.h> using namespace std; #ifndef WAIMAI #include "islands.h" #endif #ifdef WAIMAI #define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE) void dout() { cout << "\n"; } template<typename T, typename...U> void dout(T t, U...u) { cout << t << (sizeof...(u) ? ", " : ""), dout(u...); } #else #define debug(...) 7122 #endif #define ll long long #define Waimai ios::sync_with_stdio(false), cin.tie(0) #define FOR(x,a,b) for(int x = a, I = b; x <= I; x++) #define pb emplace_back #define F first #define S second const int SIZE = 1e5 + 5; int n, m, s; bool state[2 * SIZE]; vector<int> ans, chain; set<pair<int, int>> adj[SIZE], rev[SIZE]; queue<int> q; void deal() { while (q.size()) { int pos = q.front(); q.pop(); for (auto [np, id] : rev[pos]) if (adj[np].size()) { adj[np].erase({pos, id}); if (adj[np].size() == 0) q.push(np); } } } int cnt; void dfs(int pos, int last) { if (pos == s && last != -1 && cnt == 0) return; for (auto [np, id] : adj[pos]) if (id != last) { cnt -= state[id]; adj[pos].erase({np, id}); adj[np].emplace(pos, id); state[id] ^= 1; ans.pb(id); cnt += state[id]; dfs(np, id); return; } } variant<bool, vector<int>> find_journey(int N, int M, vector<int> U, vector<int> V) { n = N, m = M; FOR (i, 0, m - 1) { adj[U[i]].emplace(V[i], i); rev[V[i]].emplace(U[i], i); } FOR (i, 0, n - 1) if (adj[i].size() == 0) q.push(i); deal(); while (adj[s].size() <= 1) { q.push(s), deal(); if (adj[s].size() == 0) return false; auto [np, id] = *adj[s].begin(); chain.pb(id); s = np; } FOR (i, 0, n - 1) while (adj[i].size() > 1 + (i == s)) adj[i].erase(adj[i].begin()); dfs(s, -1); ans.insert(ans.begin(), chain.begin(), chain.end()); reverse(chain.begin(), chain.end()); ans.insert(ans.end(), chain.begin(), chain.end()); return ans; } /* in1 4 5 0 1 1 2 2 3 0 3 3 1 out1 1 10 0 1 2 4 0 3 2 1 4 3 in2 2 3 0 1 1 0 1 0 out2 0 0 */ #ifdef WAIMAI int main() { int N, M; assert(2 == scanf("%d %d", &N, &M)); vector<int> U(M), V(M); for (int i = 0; i < M; ++i) { assert(2 == scanf("%d %d", &U[i], &V[i])); } variant<bool, vector<int>> result = find_journey(N, M, U, V); if (result.index() == 0) { printf("0\n"); if (get<bool>(result)) { printf("1\n"); } else { printf("0\n"); } } else { printf("1\n"); vector<int> &canoes = get<vector<int>>(result); printf("%d\n", static_cast<int>(canoes.size())); for (int i = 0; i < static_cast<int>(canoes.size()); ++i) { if (i > 0) { printf(" "); } printf("%d", canoes[i]); } printf("\n"); } return 0; } #endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...