Submission #989076

#TimeUsernameProblemLanguageResultExecution timeMemory
989076canadavid1Monthly railway pass (LMIO18_menesinis_bilietas)C++14
100 / 100
482 ms111120 KiB
#include <iostream> #include <vector> #include <unordered_set> struct uf { private: std::vector<int> data; public: int find(int val) { if (data[val] < 0) return val; return data[val] = find(data[val]); } uf(int s) : data(s,-1) {} bool same(int a, int b) { return find(a)==find(b); } bool unite(int a, int b) { a = find(a); b = find(b); if (a==b) return false; if (-data[a] < -data[b]) std::swap(a,b); data[a] += data[b]; data[b] = a; return true; } int size(int a) { return -data[find(a)]; } }; int main() { int N,M; std::cin >> N >> M; uf uf(N); std::vector<std::pair<int,int>> n; for(int i = 0; i < M; i++) { int u,v; char c; std::cin >> u >> v >> c; u--;v--; if(c=='T') uf.unite(u,v); else n.emplace_back(u,v); } std::vector<std::unordered_set<int>> neigh(N); for(auto[u,v] : n) { u = uf.find(u); v = uf.find(v); if(u==v) continue; neigh[u].insert(v); neigh[v].insert(u); } std::vector<int> Size(N); for(int i = 0; i < N; i++) { if(i!=uf.find(i)) continue; auto& sz = Size[i]; sz = uf.size(i); for(auto n : neigh[i]) sz += uf.size(n); } int m = N; int ct = 0; for(int i = 0; i < N; i++) if(Size[i]==m) ct += uf.size(i); std::cout << ct << "\n"; }

Compilation message (stderr)

menesinis_bilietas.cpp: In function 'int main()':
menesinis_bilietas.cpp:52:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   52 |     for(auto[u,v] : n)
      |             ^
#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...