Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
k1
#include<bits/stdc++.h> using namespace std; int comp=0; map<int,map<int,int>> adj; void add_Node(int x){ adj[x]; Par[x] = x; comp = comp + 1; } // Find root of g int find_root(int node){ while(Par[node]!=node){ Par[node] = Par[Par[node]]; node = Par[i]; } return node; } void union(int a, int b) { a = find_root(a); b = find_root(b); if (a != b){ if(Rank[a] >= Rank[b]){ Rank[a] += Rank[b]; Par[b] = a; }else{ Rank[b] += Rank[a]; Par[a] = b; } } } void add_Edge(int x,int y){ adj[x][y]++; adj[y][x]++; if(find_par[x]==find_par[y]){ } } void remove_node(int x){ adj.erase(x); for(auto i:adj){ if(i.second.find(x)!=i.second.end()){ i.second[x]--; if(i.second[x]==0) i.second.erase(x); } } } //remove edge between node x and y void remove_edge(int x,int y){ if(adj[x].find(y)==adj[x].end()){ cout << "No such edge\n"; return; } adj[x][y]--; adj[y][x]--; if(adj[x][y]==0){ adj[x].erase(y); adj[y].erase(x); } } void DFS(int x){ visited[x]=1; for(auto i:adj[x]){ if(!visited[i.first]) DFS(i.first); } } int num_of_comp(){ visited.clear(); int cnt=0; for(auto i:adj){ if(!visited[i.first]){ cnt++; DFS(i.first); } } return cnt; } int main(){ int m, //Number of edges q; //Number of queries cin >> n >> m >> q; for(int i=0;i<m;i++){ int x,y; cin >> x >> y; //Edge between nodes x and y //We'll have to handle multiple edges adj[x][y]++; adj[y][x]++; } //Going over queries while(q--){ int type; cin >> type; //type: 1=> add node, 2=> add edge, 3=> remove node, 4=> remove edge if(type==1){ int x; //user enters label for node cin >> x; add_node(x); cout << "No. of connected components is: " << num_of_comp() << "\n"; } else if(type==2){ int x,y; cin >> x >> y; add_edge(x,y); cout << "No. of connected components is: " << num_of_comp() << "\n"; } else if(type==3){ int x; cin >> x; remove_node(x); cout << "No. of connected components is: " << num_of_comp() << "\n"; } else if(type==4){ int x,y; cin >> x >> y; remove_edge(x,y); cout << "No. of connected components is: " << num_of_comp() << "\n"; } } return 0; }
run
|
edit
|
history
|
help
0
Stream6
Project Euler - 113
Dar
Havel Hakimi UVA-12786 Friendship Networks
static_cast makes a copy
offsetof
a
PyramidTransitionMatrix_recursive
Switch case
series