ZOJ 3659 Conquer a New Region[长春区域赛]
并查集好题,第一次写这种类型的题#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;
#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif
/************ for topcoder by zz1215 *******************/
#define FOR(i,a,b) for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a) for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a,b) for( int i = (a) ; i >= (b) ; i --)
#define S64(a) scanf(in64,&a)
#define SS(a) scanf("%d",&a)
#define LL(a) ((a)<<1)
#define RR(a) (((a)<<1)+1)
#define pb push_back
#define CL(Q) while(!Q.empty())Q.pop()
#define MM(name,what) memset(name,what,sizeof(name))
#define MC(a,b) memcpy(a,b,sizeof(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-9;
const double pi = acos(-1.0);
const int maxn = 400011;
struct zz
{
int x;
int y;
i64 c;
bool operator < (const zz & cmp ) const
{
return c > cmp.c;
}
}zx;
int n;
int f[maxn];
i64 s[maxn];
i64 dp[maxn];
vector<zz>v;
pair<int,i64> find(int x)
{
if(x==f[x])
{
return make_pair(x,0);
}
else
{
pair<int,i64> re=find(f[x]);
dp[x]+=re.second;
f[x]=re.first;
return make_pair(f[x],dp[x]);
}
}
i64 start()
{
int now,to;
for(int i=0;i<v.size();i++)
{
now = find(v[i].x).first;
to = find(v[i].y).first;
dp[now] = s[to]*v[i].c;
f[now]=i+n+1;
dp[to] = s[now]*v[i].c;
f[to]=i+n+1;
s[i+n+1]=s[now]+s[to];
}
i64 ans=0;
for(int i=1;i<=n;i++)
{
ans = max(ans,find(i).second);
}
return ans;
}
int main()
{
while(cin>>n)
{
v.clear();
for(int i=1;i<=n-1;i++)
{
SS(zx.x);SS(zx.y);SS(zx.c);
v.pb(zx);
}
sort(v.begin(),v.end());
for(int i=1;i<=2*n-1;i++)
{
f[i]=i;
dp[i]=0;
s[i]=1;
}
cout<<start()<<endl;
}
return 0;
}
补充:软件开发 , C++ ,