Royal Questions Codeforces - 875F

In a medieval kingdom, the economic crisis is raging. Milk drops fall, Economic indicators are deteriorating every day, money from the treasury disappear. To remedy the situation, King Charles Sunnyface decided make his n sons-princes marry the brides with as big dowry as possible.

In search of candidates, the king asked neighboring kingdoms, and after a while several delegations arrived with m unmarried princesses. Receiving guests, Karl learned that the dowry of the i th princess is wi of golden coins.

Although the action takes place in the Middle Ages, progressive ideas are widespread in society, according to which no one can force a princess to marry a prince whom she does not like. Therefore, each princess has an opportunity to choose two princes, for each of which she is ready to become a wife. The princes were less fortunate, they will obey the will of their father in the matter of choosing a bride.

Knowing the value of the dowry and the preferences of each princess, Charles wants to play weddings in such a way that the total dowry of the brides of all his sons would be as great as possible. At the same time to marry all the princes or princesses is not necessary. Each prince can marry no more than one princess, and vice versa, each princess can marry no more than one prince.

Help the king to organize the marriage of his sons in the most profitable way for the treasury.

Input

The first line contains two integers n, m (2 ≤ n ≤ 200 000, 1 ≤ m ≤ 200 000) — number of princes and princesses respectively.

Each of following m lines contains three integers ai, bi, wi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ wi ≤ 10 000) — number of princes, which i-th princess is ready to marry and the value of her dowry.

Output

Print the only integer — the maximum number of gold coins that a king can get by playing the right weddings.

Examples
Input
2 3
1 2 5
1 2 1
2 1 10
Output
15
Input
3 2
1 2 10
3 2 20
Output
30

题意:n个王子,m个公主,给出第i个公主可以嫁给的两个王子ai,bi和嫁妆wi,问能得到的最大嫁妆?

题解:将ai和bi连一条边,权值为wi,选择ai的话,这条边的方向指向ai,bi同理。最终,合理的情况下一定是每个连通块中0<=V-E<=1,既最多只有一个环。这个建图真是太机智了。。。
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int maxn=2e5+5;
 5 
 6 struct node{
 7     int a,b,va;
 8     bool operator<(const node& i)const{
 9         return va>i.va;
10     }
11 }p[maxn];
12 
13 int n,m;
14 int F[maxn];
15 bool P[maxn];
16 
17 int Find(int x){
18     if(x!=F[x]) F[x]=Find(F[x]);
19     return F[x];
20 }
21 
22 void Solve(){
23     int ans=0;
24     for(int i=1;i<=m;i++){
25         int x=Find(p[i].a);
26         int y=Find(p[i].b);
27         if(x!=y){
28             if(P[x]&&P[y]) continue;
29             P[x]=P[x]|P[y];
30             F[y]=x;
31             ans+=p[i].va;
32         }
33         else if(!P[x]){
34             ans+=p[i].va;
35             P[x]=1;
36         }
37     } 
38     cout<<ans<<endl;
39 }
40 
41 int main()
42 {   cin>>n>>m;
43     for(int i=1;i<=n;i++) F[i]=i;
44     for(int i=1;i<=m;i++) scanf("%d%d%d",&p[i].a,&p[i].b,&p[i].va);
45     sort(p+1,p+m+1);
46     Solve();
47 }

 

posted @ 2017-10-20 23:09  天之道,利而不害  阅读(486)  评论(0编辑  收藏  举报