Submission #373771

#TimeUsernameProblemLanguageResultExecution timeMemory
373771gustasonMonthly railway pass (LMIO18_menesinis_bilietas)C++11
0 / 100
3 ms2284 KiB
#include<bits/stdc++.h> using namespace std; using ll = long long; //const int maxN = 5e5 + 5; const int maxN = 2e4 + 5; vector<int> adj[maxN]; bool ok[maxN]; bool vis[maxN]; int parent[maxN]; int sz[maxN]; int find(int v) { if (v == parent[v]) return v; return parent[v] = find(parent[v]); } void unite(int a, int b) { a = find(a), b = find(b); if (a == b) return; if (sz[a] < sz[b]) swap(a, b); parent[b] = a; sz[a] += sz[b]; } void dfs(int v) { vis[v] = true; for(int u : adj[v]) { ok[v] |= ok[u]; if (!vis[u]) { dfs(u); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; for(int i = 1; i <= n; i++) { parent[i] = i; sz[i] = 1; } for(int i = 0; i < m; i++) { char c; int a, b; cin >> a >> b >> c; if (c == 'T') { unite(a, b); } else { adj[a].push_back(b); adj[b].push_back(a); } } set<int> s; for(int i = 1; i <= n; i++) { s.insert(parent[i]); } int comp = s.size(); for(int i = 1; i <= n; i++) { s.clear(); s.insert(parent[i]); for(int j : adj[i]) { s.insert(parent[j]); } int sum = s.size(); if (sum == comp) { ok[i] = true; } } for(int i = 1; i <= n; i++) { ok[parent[i]] |= ok[i]; } for(int i = 1; i <= n; i++) { ok[i] |= ok[parent[i]]; } int ans = 0; for(int i = 0; i <= n; i++) { ans += ok[i]; } cout << ans << "\n"; return 0; }
#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...