hdu3635(代码片段)

zsben991126 zsben991126     2023-01-09     213

关键词:

/*
一开始第a个球在第a个城市 
操作T a b,把第a个球所在城市的所有球移到b所在的城市 
操作Q a 
要求输出
第a个球在哪个城市
第a个球所在的城市有几个球 
第a个球移动次数 
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#define move movee
#define MAXN 10005
using namespace std;
//第i个球所在城市,第i个球所在城市有几个球,第i个球移动次数 
int F[MAXN],num[MAXN],move[MAXN];
int find(int a)
    if(F[a]==-1) return a;
    int tmp=F[a];//保存a的父亲 
    F[a]=find(F[a]);//
    move[a]+=move[tmp];
    return F[a]; 

//把a所在的集合移到b所在的集合 
void bing(int a,int b)
    int t1=find(a);
    int t2=find(b);
    if(t1!=t2)
        F[t1]=t2;
        move[t1]=1;
        num[t2]+=num[t1];
     
 
int main()
    int T,n,q;
    cin >> T;
    for(int tt=1;tt<=T;tt++)
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++)
            F[i]=-1;
            num[i]=1;
            move[i]=0; 
        
        char op[10];
        int a,b;
        printf("Case %d:
",tt);
        while(q--)
            scanf("%s",op);
            if(op[0]==T)
                scanf("%d%d",&a,&b);
                bing(a,b);
            
            else 
                scanf("%d",&a);
                int tmp=find(a);
                printf("%d %d %d
",tmp,num[tmp],move[a]);
            
        
     
    return 0;
 

 

hdu-3635dragonballs并查集(代码片段)

题意:1~N个龙珠,放在1~N个城市,有两种操作:TAB将A所再城市的所有球转移到B所在的城市。QX询问X所在的城市pls,该城市中龙珠数量nm,以及龙珠转移次数trs题解:并查集模板题,顺带更新一些数据。pls不必更新,因为X所在的... 查看详情

hdu3635dragonballs(带权并查集)

...#20540;是该城市的龙珠数。times[x]为该龙珠运输了多少次。代码例如以下:#inclu 查看详情

hdu3635dragonballs(带权并查集)

题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=3635题目描述:DragonBallsProblemDescriptionFivehundredyearslater,thenumberofdragonballswillincreaseunexpectedly,soit‘stoodifficultforMonkeyKing(WuKong)togat 查看详情

hdu3635dragonballs

DragonBallsTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):4165    AcceptedSubmission(s):1603ProblemDescriptionFiv 查看详情

hdu3635

题目链接:HDU-36351#include<cstdio>2#include<cstring>3constintmaxn=10010;4intf[maxn],ct[maxn],trans[maxn];5intn,m;6voidinit()7{8for(inti=0;i<=n;i++)9{10f[i]=i;11ct[i]=1;12trans[i]=0;13}14}15 查看详情

hdu3635dragonballs(并查集)

DragonBallsTimeLimit:2000/1000ms(Java/Other)   MemoryLimit:32768/32768K(Java/Other)TotalSubmission(s):64   AcceptedSubmission(s):26Font: TimesNewRoman | Ve 查看详情

poj3635fulltank?(代码片段)

...求最短路。同时考虑在每个节点加油(一单位)与否。【代码】1#include<iostream>2#include<map>3#include<cstring>4#include<string>5#include<queue>6usingnamespacestd;7#definemaxn10058#definemaxm10005910/*11*dp[i][j]denodestheleastcostontheithnode12*... 查看详情

3635转换成小写字母(代码片段)

给你一个字符串s,将该字符串中的大写字母转换成相同的小写字母,返回新的字符串。示例1:输入:s="Hello"输出:"hello"示例2:输入:s="here"输出:"here"示例3:输... 查看详情

hdu4499(代码片段)

InChineseChess,thereisonekindofpowerfulchessmencalledCannon.Itcanmovehorizontallyorverticallyalongthechessgrid.Ateachmove,itcaneithersimplymovetoanotheremptycellinthesamelinewithoutanyotherchessmanalo 查看详情

hdu1501zipper(代码片段)

题目链接:HDU1501DescriptionGiventhreestrings,youaretodeterminewhetherthethirdstringcanbeformedbycombiningthecharactersinthefirsttwostrings.Thefirsttwostringscanbemixedarbitrarily,buteachmuststayinitsorigi 查看详情

hdu2053(代码片段)

SwitchGameHDU-2053Timelimit1000msMemorylimit32768kBTherearemanylampsinaline.Allofthemareoffatfirst.Aseriesofoperationsarecarriedoutontheselamps.Onthei-thoperation,thelampswhosenumbersarethemultipleofi 查看详情

hdu1016primeringproblem(代码片段)

题目链接:HDU1016DescriptionAringiscomposeofncirclesasshownindiagram.Putnaturalnumber1,2,...,nintoeachcircleseparately,andthesumofnumbersintwoadjacentcirclesshouldbeaprime.Note:thenumberoffirstcircleshould 查看详情

hdu1548astrangelift(代码片段)

题目链接:HDU1548DescriptionThereisastrangelift.Theliftcanstopcanateveryfloorasyouwant,andthereisanumberKi(0<=Ki<=N)oneveryfloor.Thelifthavejusttwobuttons:upanddown.Whenyouatfloori,ifyoupressthebutto 查看详情

hdu4283youaretheone(代码片段)

题目:YouAretheOne网址:http://acm.hdu.edu.cn/showproblem.php?pid=4283ProblemDescription  TheTVshowssuchasYouAretheOnehasbeenverypopular.Inordertomeettheneedofboyswhoarestillsingle,TJUTholdtheshowitself.The 查看详情

hdu5459(代码片段)

ProblemDescriptionI‘vesentFangFangaround201314textmessagesinalmost5years.Whycan‘tshemakesenseofwhatImean?``ButJesusishere!"thepriestintoned.``Showmeyourmessages."Fine,thefirstmessageis s1=‘‘c"&nb 查看详情

hdu2795billboard(代码片段)

hdu2795BillboardTimeLimit:20000/8000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795ProblemDescriptionAttheentrancetotheuni 查看详情

hdu1251统计难题(字典树)(代码片段)

目录问题分析代码问题hdu1251统计难题-https://acm.hdu.edu.cn/showproblem.php?pid=1251分析trie[0]作为根代码/*hdu1251统计难题*/#include<bits/stdc++.h>usingnamespacestd;constintMXN=500010;inttrie[MXN][26],num 查看详情

hdu1885keytask(代码片段)

TheCzechTechnicalUniversityisratherold—youalreadyknowthatitcelebrates300yearsofitsexistencein2007.Someoftheuniversitybuildingsareoldaswell.Andthenavigationinoldbuildingscansometimesbealittlebittricky, 查看详情