UVALive 4848 Tour Belt

F - Tour Belt

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status Practice UVALive 4848

Description

Korea has many tourist attractions. One of them is an archipelago (Dadohae in Korean), a cluster of small islands scattered in the southern and western coasts of Korea. The Korea Tourism Organization (KTO) plans to promote a new tour program on these islands. For this, The KTO wants to designate two or more islands of the archipelago as a tour belt.

There are n islands in the archipelago. The KTO focuses on the synergy effect created when some islands are selected for a tour belt. Synergy effects are assigned to several pairs of islands. A synergy effect SE(uv) or SE(vu) between two islands u and v is a positive integer which represents a value anticipated when both u and v are included in a tour belt. The KTO wants to select two or more islands for the tour belt so that the economic benefit of the tour belt is as high as possible.

To be precise, we define a connected graph G = (VE), where V is a set of n vertices and E is a set of m edges. Each vertex of V represents an island in the archipelago, and an edge (uv) of E exists if a synergy effect SE(uv) is defined between two distinct vertices (islands) uand v of V. Let A be a subset consisting of at least two vertices in V. An edge (uv) is an inside edge of A if both u and v are in A. An edge(uv) is a border edge of A if one of u and v is in A and the other is not in A.

A vertex set B of a connected subgraph of G with 2B|n is called a candidate for the tour belt if the synergy effect of every inside edge of B is larger than the synergy effect of any border edge of B. A candiate will be chosen as the final tour belt by the KTO. There can be many possible candidates in G. Note that V itself is a candidate because there are no border edges. The graph in Figure 1(a) has three candidates {1,2}, {3,4} and {1,2,3,4}, but {2,3,4} is not a candidate because there are inside edges whose synergy effects are not larger than those of some border edges. The graph in Figure 1(b) contains six candidates, {1,2}, {3,4}, {5,6}, {7,8}, {3,4,5,6} and {1,2,3,4,5,6,7,8}. But {1,2,7,8} is not a candidate because it does not form a connected subgraph of G, i.e., there are no edges connecting {1,2} and {7,8}.

Figure 1. Graphs and their good subsets marked by gray ellipses.

The KTO will decide one candidate in G as the final tour belt. For this, the KTO asks you to find all candidates in G. You write a program to print the sum of the sizes of all candidates in a given graph G. For example, the graph in Figure 1(a) contains three candidates and the sum of their sizes is 2 + 2 + 4 = 8, and the graph in Figure 1(b) contains six candidates and the sum of their sizes is 2 + 2 + 2 + 2 + 4 + 8 = 20.

Input

Your program is to read input from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing two integers n ( 2n5, 000) and m ( 1m), where n represents the number of vertices (islands) and m represents the number of edges of a connected graph G. Islands are numbered from 1 to n. In the following m lines, the synergy effects assigned to m edges are given; each line contains three integers, uv, and k ( 1uvn, 1k105), where k is the synergy effect between two distinct islands u and v, i.e., SE(uv) = SE(uv) = k.

Output

Your program is to write to standard output. Print exactly one line for each test case. Print the sum of the sizes of all candidates for a test case.

The following shows sample input and output for two test cases.

Sample Input

2
4 6
1 2 3
2 3 2
4 3 4
1 4 1
2 4 2
1 3 2
8 7
1 2 2
2 3 1
3 4 4
4 5 3
5 6 4
6 7 1
7 8 2

Sample Output

8
20

题意:在一个联通无向图里面找一个联通子图,如果这个联通子图里面的最小边权大于子图和非子图的连边的最大值,那么ans += 联通子图的点数目。输出ans。思路:按边从大到小排序,然后一个个加进来,如果原来这条边的两个点是联通的,那么不理不连通就去判断是否符合条件,(纯暴力。。)

#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<algorithm>
#define INF 0x3f3f3f3f
#define FINF 1e9
#define ll long long
#define BUG printf("hehe!~\n")
using namespace std;

const int N=5010;
const int M=22501000;

struct Edge
{
    int u,v,w;
}edge[M];

bool cmp(Edge A,Edge B)
{
    return A.w>B.w;
}

int cur[N+N];
int n,m;
int fa[N];

int findp(int x) {
    return fa[x]==x?x:fa[x]=findp(fa[x]);
}

void init()
{
    for(int i=0;i<=n;++i) fa[i]=i,cur[i]=1;
}

int connect(int fu,int fv)
{
    cur[fu]+=cur[fv];
    return cur[fu];
}

int main()
{
    int _;
    cin>>_;
    int u,v,w;
    int x,y,num;
    bool flag;
    int ans,cnt;
    while(_--) {
        scanf("%d%d",&n,&m);
        init();
        ans=0;
        for(int i=0;i<m;++i) {
            scanf("%d%d%d",&u,&v,&w);
            edge[i].u=u, edge[i].v=v, edge[i].w=w;
        }
        sort(edge,edge+m,cmp);
        num=n;
        for(int i=0;i<m;++i) {
            u=edge[i].u,v=edge[i].v,w=edge[i].w;
            int fu=findp(u);
            int fv=findp(v);
            if(fu!=fv) {
                num++;
                fa[fv]=num;
                fa[fu]=num;

                fa[num]=num;
                flag=true;
                cur[num]=cur[fv]+cur[fu];
                cnt=cur[num];

                int mn=INF,mx=-INF;
                for(int j=0;j<m;++j) {
                    Edge& e=edge[j];
                    x=findp(e.u);y=findp(e.v);
                    if(x==y&&x==num) mn=min(mn,e.w);
                    else if(x==num||y==num) {
                        mx=max(mx,e.w);
                    }
                }
                if(mn>mx) {
                    ans+=cnt;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

时间: 2024-10-11 06:10:33

UVALive 4848 Tour Belt的相关文章

UVALive 4035 - Undetectable Tour(并查集)

题意:给定一个 N * N(3 <= N <= 10000)的矩形区域,左下角为(0,0),右上角为(N,N),现在要从左下角走到右上角,但是有 k(k <= 100)个监视器,每个监视器的监视范围都是统一的,现给定监视范围可能出现的种类与概率,求能够逃出去的概率.(计算距离时均用曼哈顿距离) 1.若两个监视器的距离 <= 两个监视器的监视范围之和,则这两个监视器间的路就不可走: 2.若监视器距离墙的距离 <= 监视器的监视范围,则监视器与墙之间就不可走: 综上,对于每一个监

UVaLive 6853 Concert Tour (DP)

题意:给定 n 个城市,m 个月,表示要在这 n 个城市连续 m 个月开演唱会,然后给定每个月在每个城市开演唱会能获得的利润,然后就是演唱会在不同城市之间调动所要的费用, 问你,怎么安排这 n 个演唱会是最优的. 析:很明显的一个DP题,并且也不难,用dp[i][j] 表示在第 i 个月,在第 j 个城市开演唱会,是最优的.那么状态转移方程也就出来了 dp[i][j] = Max(dp[i][j], dp[i-1][k]-f[k][j]+p[j][i]); 代码如下: #pragma comme

F - Free DIY Tour(动态规划)

这道题也可以用深搜做,可以深搜本来就不熟,好久没做早忘了,明天看看咋做的 Description Weiwei is a software engineer of ShiningSoft. He has just excellently fulfilled a software project with his fellow workers. His boss is so satisfied with their job that he decide to provide them a free

HDU 1853 Cyclic Tour(最小费用最大流)

Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Total Submission(s): 1879    Accepted Submission(s): 938 Problem Description There are N cities in our country, and M one-way roads connecting them. Now L

UVALive 6467 Strahler Order 拓扑排序

这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ 以后能用CIN还是CIN吧 QAQ 贴代码了: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostre

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

Codeforces 490F Treeland Tour(离散化 + 线段树合并)

题目链接 Treeland Tour 题目就是让你求树上LIS 先离散化,然后再线段树上操作.一些细节需要注意一下. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i) #define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const int N =

hdoj 3488 Tour 【最小费用最大流】【KM算法】

Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2299    Accepted Submission(s): 1151 Problem Description In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 3000

HDU3488 Tour [有向环覆盖 费用流]

Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 3159    Accepted Submission(s): 1525 Problem Description In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000