题意大概是给一个双向边的图,不一定联通,好吧反正就是并查集。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <bitset>
#include <climits>
#include <map>
using namespace std;
int p[1005],G[1005],r[10005];
int n,m,a,b;
int find(int x)
{
return p[x]==x?x:p[x]=find(p[x]);
}
map <int,int> mp;
vector <pair <int,int> > v;
vector <int> f;
bool cmp(pair <int,int> a,pair <int,int> b)
{
return a.second>b.second;
}
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0);
//freopen("in.txt","r",stdin);
int T;scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
mp.clear();v.clear();f.clear();
for(int i=0;i<n;++i)p[i]=i;
for(int i=0;i<m;++i)r[i]=i;
for(int i=0;i<n;++i)
{
scanf("%d",G+i);
}
int x,y;
for(int i=0;i<m;++i)
{
scanf("%d%d",&a,&b);
--a;--b;
x=find(a);y=find(b);
if(x!=y)p[x]=y;
}
int maxx=-1;
for(int i=0;i<n;++i)
{
x=find(i);
p[i]=x;
mp[x]+=G[i];
maxx=max(maxx,mp[x]);
}
map <int,int> ::iterator it=mp.begin();
while(it!=mp.end())
{
if(it->second==maxx)f.push_back(it->first);
++it;
}
int len=f.size();
for(int i=0;i<n;++i)
{
for(int j=0;j<len;++j)
{
if(p[i]==f[j])v.push_back(make_pair(i,G[i]));
}
}
sort(v.begin(),v.end(),cmp);
printf("%d\n",v[0].first+1);
}
return 0;
}