福工周赛第二场 并查集

3262

//#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include 
#define lowbit(x) (x&(-x))
#define inf  0x7fffffff
#define linf 0x7fffffffffffffff
#define fil(x,y) memset(x,y,sizeof(x))
#define fup(i,x,y) for(int i=(x);i<=(y);i++)
#define fdn(i,x,y) for(int i=(x);i>=(y);i--)
#define sp(x) setprecision(x)
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define sc(n) scanf("%s",n)
#define pf(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
#define pff(x) printf("%lf\n",x)
#define debug printf("!!\n");
#define N 1000005
#define M 4000009

#define pi acos(-1)
#define eps 1e-2
//cout.setf(ios::fixed);
//freopen("out.txt","w",stdout);// freopen("in.txt","r",stdin);
using namespace std;
typedef long long  ll;
typedef double db;
int fa[100005],siz[100005];
ll sum[200005];
struct node
{
    int l,r,t;
    bool operator <(const node b) const
    {
        return t200005];
struct qur
{
    int stand,id;
    bool operator <(const qur b) const
    {
        return stand200005];
int findfa(int x)
{
    return x==fa[x] ? x:fa[x]=findfa(fa[x]);
}
int Union(int x,int y)
{
    int xx=findfa(x);
    int yy=findfa(y);
    if(xx==yy) return 0;
    int ans=siz[xx]*siz[yy];
   // pf(fa[xx]);
    if(siz[xx]>siz[yy])
    {
        siz[xx]+=siz[yy];
        fa[yy]=xx;
    }
    else
    {
       siz[yy]+=siz[xx];
       fa[xx]=yy;
    }
    return ans;
}
int main()
{
    int n,m,q;
    sddd(n,m,q);
    fup(i,1,n)
        fa[i]=i,siz[i]=1;
    fup(i,1,m)
        sddd(a[i].l,a[i].r,a[i].t);
    fup(i,1,q)
        sd(qu[i].stand),qu[i].id=i;
    sort(a+1,a+1+m);
    sort(qu+1,qu+1+q);
    int st=1;
    ll ans=0;
    fup(i,1,q)
    {
        while(st<=m&&a[st].t<=qu[i].stand)
        {
            ans+=Union(a[st].l,a[st].r);
            st++;
        }
        sum[qu[i].id]=ans;
    }
    fup(i,1,q)
        pfl(sum[i]);
    return 0;
}

你可能感兴趣的:(并查集)