Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define pb push_back
- #define ll long long
- #define pii pair<int,int>
- #define pll pair<ll,ll>
- //#define M 100007
- #define INF 1e9
- #define INFL 1e18
- #define PI acos(-1)
- #define mp make_pair
- #define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
- #include<bits/stdc++.h>
- using namespace std;
- #define M 10000
- #define ll long long
- ll pr[M];
- struct edge
- {
- long long u,v,w;
- bool operator<(const edge& p)const
- {
- return w<p.w;
- }
- };
- vector<edge>e,tree;
- long long root(long long i)
- {
- while(pr[i]!=i)
- {
- i=pr[i];
- }
- return i;
- }
- void mst(ll n)
- {
- sort(e.begin(),e.end());
- for(int i=0;i<=n;i++)
- pr[i]=i;
- int cnt=0,s=0;
- for(int i=0;i<e.size();i++)
- {
- ll u=root(e[i].u);
- ll v=root(e[i].v);
- if(u!=v)
- {
- pr[u]=v;
- cnt++;
- char a=e[i].u+'A';
- char b=e[i].v+'A';
- ll c=e[i].w;
- if(a>b)
- swap(a,b);
- cout<<a<<'-'<<b<<' '<<c<<endl;
- if(cnt==n-1)
- break;
- }
- }
- }
- int myfunc(char c[])
- {
- int num=0;
- for(int i=0;i<strlen(c);i++)
- {
- num=num*10+(c[i]-'0');
- }
- return num;
- }
- int main()
- {
- fast_in_out;
- int t,kase=0;
- cin>>t;
- while(kase<t)
- {
- int n;
- cin>>n;
- vector<int>v[n+5];
- for(int i=0;i<n;i++)
- {
- char str[10000];
- gets(str);
- char *token=strtok(str," ,");
- while(token!=NULL)
- {
- int num=myfunc(token);
- v[i].pb(num);
- token=strtok(NULL," ,");
- }
- }
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<v[i].size();j++)
- {
- if(v[i][j]!=0)
- {
- edge E;
- E.u=i;
- E.v=j;
- E.w=v[i][j];
- e.pb(E);
- }
- }
- }
- cout<<"Case "<<kase+1<<":"<<endl;
- mst(n);
- kase++;
- e.clear();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment