-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlightoj 1041(kruskal).cpp
More file actions
162 lines (117 loc) · 3.05 KB
/
lightoj 1041(kruskal).cpp
File metadata and controls
162 lines (117 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
///***Bismillahir Rahmanir Rahim***///
///*********************************///
///******Ashraful Haque Toni********///
///********Dept. of CSE,JnU*********///
///email:ashrafulhaquetoni@gmail.com///
///*******contact:01640690531*******///
///*********************************///
#include<bits/stdc++.h>
using namespace std;
#define ash ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define sc(n) scanf("%d",&n);
#define scl(n) scanf("%I64d",&n);
#define sc2(m,n) scanf("%d%d",&m,&n);
#define sc2l(m,n) scanf("%I64d%I64d",&m,&n);
#define pf printf
#define Big(x,y) max(x,y);
#define Small(x,y) min(x,y);
#define input(array,size) for(int i=0;i<size;i++)cin>>array[i];
#define newline pf("\n");
#define f(s,l,in) for(ll i=s;i<l;i+=in)
#define Max INT_MAX
#define Min INT_MIN
#define pi acos(-1.0)
#define Memset(a,val) memset(a,val,sizeof(a));
const int mod =1e9+7;
const int N =5e6+5;
typedef long long ll;
vector<int>parent(55);
int Fparent(int x)
{
return (x==parent[x])?x:(parent[x]=Fparent(parent[x]));
}
void Union(int x,int y)
{
parent[Fparent(x)]=parent[Fparent(y)];
// cout<<parent[x]<<endl;
}
int kruskal(vector<pair<int,pair<int,int> > >g,int n)
{
int k=0;
//cout<<g.size()<<endl;
for(int i=0; i<g.size(); i++)
{
int U=g[i].second.first;
int V=g[i].second.second;
int W=g[i].first;
// cout<<"("<<U<<","<<V<<","<<W<<")";
//cout<<Fparent(U)<<Fparent(V)<<endl;
if(Fparent(U)!=Fparent(V))
{
k+=W;
//cout<<"("<<U<<","<<V<<","<<W<<")";
Union(U,V);
}
}
int l=Fparent(1);
for(int i=2; i<=n; i++)
{
if(Fparent(i)!=l)
return -1;
}
return k;
}
void initialize()
{
for(int i=0; i<55; i++)
{
parent[i]=i;
}
}
int main()
{
//freopen("1041.txt","r",stdin);
ash;
int t;
cin>>t;
for(int ii=1; ii<=t; ii++)
{
int node,k=1;
cin>>node;
map<string,int>m;
vector<pair<int,pair<int,int> > >g;
while(node--)
{
string s,s1;
int we;
cin>>s>>s1>>we;
if(m.find(s)==m.end())
{
m[s]=k++;
}
if(m.find(s1)==m.end())
{
m[s1]=k++;
}
g.pb({we,{m[s],m[s1]}});
//cout<<m[s]<<m[s1]<<endl;
}
sort(g.begin(),g.end());
//cout<<m.size()<<endl;
initialize();
int kk=kruskal(g,m.size());
cout<<"Case "<<ii<<": ";
if(kk==-1)
{
cout<<"Impossible\n";
}
else
{
cout<<kk<<endl;
}
//graph.clear();
}
return 0;
}