hdu 6187(并查集

来源:互联网 发布:交通大数据 编辑:程序博客网 时间:2024/05/17 22:41


Destroy Walls

                                                              Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
                                                                                             Total Submission(s): 175    Accepted Submission(s): 63


Problem Description
Long times ago, there are beautiful historic walls in the city. These walls divide the city into many parts of area. 

Since it was not convenient, the new king wants to destroy some of these walls, so he can arrive anywhere from his castle. We assume that his castle locates at (0.62,0.63)

There are n towers in the city, which numbered from 1 to n. The ith's location is (xi,yi). Also, there are m walls connecting the towers. Specifically, the ith wall connects the tower ui and the tower vi(including the endpoint). The cost of destroying the ith wall is wi

Now the king asks you to help him to divide the city. Firstly, the king wants to destroy as less walls as possible, and in addition, he wants to make the cost least. 

The walls only intersect at the endpoint. It is guaranteed that no walls connects the same tower and no 2 walls connects the same pair of towers. Thait is to say, the given graph formed by the walls and towers doesn't contain any multiple edges or self-loops.

Initially, you should tell the king how many walls he should destroy at least to achieve his goal, and the minimal cost under this condition.
 

Input
There are several test cases.

For each test case:

The first line contains 2 integer n, m.

Then next n lines describe the coordinates of the points.

Each line contains 2 integers xi,yi

Then m lines follow, the ith line contains 3 integers ui,vi,wi

|xi|,|yi|105

3n100000,1m200000

1ui,vin,uivi,0wi10000
 

Output
For each test case outout one line with 2 integers sperate by a space, indicate how many walls the king should destroy at least to achieve his goal, and the minimal cost under this condition. 
 

Sample Input
4 4-1 -1-1 11 11 -11 2 12 3 23 4 14 1 2
 

Sample Output
1 1
 

Source
2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)

要想国王能达到任何一个地方,那么一定不能有环,就是求一棵树,同时拆掉的边的值最小,就是求一个最大生成树
直接对原图的每个连通块求最大生成树,最后不在最大生成树上的边就是要拆的墙。


//china no.1#pragma comment(linker, "/STACK:1024000000,1024000000")#include <vector>#include <iostream>#include <string>#include <map>#include <stack>#include <cstring>#include <queue>#include <list>#include <stdio.h>#include <set>#include <algorithm>#include <cstdlib>#include <cmath>#include <iomanip>#include <cctype>#include <sstream>#include <functional>#include <stdlib.h>#include <time.h>#include <bitset>using namespace std;#define pi acos(-1)#define s_1(x) scanf("%d",&x)#define s_2(x,y) scanf("%d%d",&x,&y)#define s_3(x,y,z) scanf("%d%d%d",&x,&y,&z)#define PI acos(-1)#define endl '\n'#define srand() srand(time(0));#define me(x,y) memset(x,y,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)#define close() ios::sync_with_stdio(0); cin.tie(0);#define FOR(x,n,i) for(int i=x;i<=n;i++)#define FOr(x,n,i) for(int i=x;i<n;i++)#define fOR(n,x,i) for(int i=n;i>=x;i--)#define fOr(n,x,i) for(int i=n;i>x;i--)#define W while#define sgn(x) ((x) < 0 ? -1 : (x) > 0)#define bug printf("***********\n");#define db double#define ll long long#define mp make_pair#define pb push_backtypedef long long LL;const int INF=0x3f3f3f3f;const LL LINF=0x3f3f3f3f3f3f3f3fLL;const int dx[]={-1,0,1,0,1,-1,-1,1};const int dy[]={0,1,0,-1,-1,1,-1,1};const int maxn=1e3+10;const int maxx=2e5+100;const double EPS=1e-8;const double eps=1e-8;const int mod=1e9+7;template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}template <class T>inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;}while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');while (c = getchar(), c >= '0' && c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;}inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false;while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;}else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}}if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}}if(IsN) num=-num;return true;}void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}void print(LL a){ Out(a),puts("");}//freopen( "in.txt" , "r" , stdin );//freopen( "data.txt" , "w" , stdout );//cerr << "run time is " << clock() << endl;struct node{    int u,v,w;}Q[maxx];int cnt;int fa[maxx];bool cmp(node a,node b){    return a.w>b.w;}int fi(int x){    return fa[x]==x?x:fa[x]=fi(fa[x]);}int n,m;int main(){    W(scanf("%d%d",&n,&m)!=EOF)    {        cnt=0;        me(Q,0);        int x;        FOR(1,n,i)        {            s_1(x);            scan_d(x);        }        LL ans=0;        FOR(1,n,i)            fa[i]=i;        FOR(1,m,i)        {            scan_d(Q[i].u);            scan_d(Q[i].v);            scan_d(Q[i].w);            ans+=Q[i].w;        }        sort(Q+1,Q+m+1,cmp);        FOR(1,m,i)        {            int u=fi(Q[i].u);            int v=fi(Q[i].v);            if(u!=v)            {                fa[u]=v;                ans-=Q[i].w;                //cout<<ans<<endl;            }            else cnt++;        }        cout<<cnt<<" "<<ans<<endl;    }}



原创粉丝点击