Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstring>
- #include <vector>
- #include <list>
- #include <map>
- #include <set>
- #include <deque>
- #include <stack>
- #include <bitset>
- #include <algorithm>
- #include <functional>
- #include <numeric>
- #include <utility>
- #include <sstream>
- #include <iostream>
- #include <iomanip>
- #include <cstdio>
- #include <cmath>
- #include <cstdlib>
- #include <ctime>
- #include <memory.h>
- #include <cassert>
- using namespace std;
- struct node {
- string id;
- string s1,s2;
- int cost;
- };
- class comp {
- public:
- bool operator () (node a,node b){
- if (a.cost != b.cost)
- return a.cost>b.cost;
- return a.id > b.id;
- }
- };
- map<string ,int > mp;
- priority_queue < node, vector<node>, comp > pq;
- // I will use the set for storing the various values of the
- // conditions
- int ct=0;
- int hash(string str){
- int idx=0;
- idx=mp[str];
- if( idx==0 ){
- idx=ct;
- mp[str]=ct++;
- }
- return idx;
- }
- int array[1000000];
- void makeset(){
- for (int i=0; i<ct ;i++)
- array[i]=-1;
- }
- int findpar(int x){
- int y=x,par;
- while (array[x] != -1) {
- x=array[x];
- }
- par=x;
- int t;
- while (array[y] != -1) {
- t=y;
- y=array[y];
- array[t]=par;
- }
- return par;
- }
- int mergeset(int x,int y){
- if (findpar(x) != findpar(y)) {
- array[x]=y;
- return 1;
- }
- return 0;
- }
- bool valid (node temp){
- if ( mergeset( mp[temp.s1] ,mp[temp.s2] ) ){
- if (temp.cost!=0)
- return true;
- }
- return false;
- }
- void kruskal(){
- node temp;
- while ( !pq.empty() ) {
- temp=pq.top();
- pq.pop();
- if ( valid( temp ) )
- cout << temp.id<<endl;
- }
- }
- int main() {
- int t;
- cin >>t;
- while (t-- ){
- int n,m;
- cin >>n>>m;
- node temp;
- for (int i=0; i<n ;i++ ){
- cin>>temp.id>>temp.s1>>temp.s2;
- hash(temp.s1);
- hash(temp.s2);
- temp.cost=0;
- pq.push(temp);
- }
- for (int i=0; i<m ;i++ ){
- cin>>temp.id>>temp.s1>>temp.s2>>temp.cost;
- hash(temp.s1);
- hash(temp.s2);
- pq.push(temp);
- }
- makeset();
- kruskal();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement