Submission #382155

#TimeUsernameProblemLanguageResultExecution timeMemory
382155MohamedAhmed04Monthly railway pass (LMIO18_menesinis_bilietas)C++14
100 / 100
738 ms93736 KiB
#include <bits/stdc++.h> using namespace std ; const int MAX = 5e5 + 10 ; int arr[MAX] ; int n , m ; vector< vector< pair<int , char> > >adj(MAX) ; int par[MAX] , sz[MAX] ; vector<int>v[MAX] ; void init() { for(int i = 1 ; i <= n ; ++i) par[i] = i , sz[i] = 1 ; } int root(int node) { if(par[node] == node) return node ; return (par[node] = root(par[node])) ; } void Union(int x , int y) { int a = root(x) ; int b = root(y) ; if(a == b) return ; if(sz[a] < sz[b]) swap(a , b) ; par[b] = a ; sz[a] += sz[b] ; } int main() { ios_base::sync_with_stdio(0) ; cin.tie(0) ; cin>>n>>m ; init() ; for(int i = 0 ; i < m ; ++i) { int x , y ; char c ; cin>>x>>y>>c ; if(c == 'T') Union(x , y) ; adj[x].emplace_back(y , c) ; adj[y].emplace_back(x , c) ; } set<int>s ; for(int i = 1 ; i <= n ; ++i) s.insert(root(i)) , v[root(i)].push_back(i) ; set<int>s2 ; int ans = 0 ; for(int i = 1 ; i <= n ; ++i) { if(!v[i].size()) continue ; s2.clear() ; s2.insert(i) ; for(auto &node : v[i]) { for(auto &child : adj[node]) s2.insert(root(child.first)) ; } ans += (s.size() == s2.size()) * v[i].size() ; } return cout<<ans<<"\n" , 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...