Exchanging Gifts--2019CCPC哈尔滨 E题
题意:http://codeforces.com/gym/102394/problem/E
1操作是给你一串数,2操作是连结两个串(所以可能很长),问你最后一个串的值(知道最多的个数就很好算,关键计算个数)
思路:
对二操作建图,一开始还以为建出来的是树就可以直接BFS计算次数,自闭了好久,最后才发现是拓扑图,一点点扒点才行。
离散化加统计个数啥的导致代码冗长,奇奇怪怪的数组贼多。
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
#include <bitset>
//#include <map>
#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
#include <cassert>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
//******************
int abss(int a);
int lowbit(int n);
int Del_bit_1(int n);
int maxx(int a,int b);
int minn(int a,int b);
double fabss(double a);
void swapp(int &a,int &b);
clock_t __STRAT,__END;
double __TOTALTIME;
void _MS(){__STRAT=clock();}
void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
//***********************
#define rint register int
#define fo(a,b,c) for(rint a=b;a<=c;++a)
#define fr(a,b,c) for(rint a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef long long ll;
const double E=2.718281828;
const double PI=acos(-1.0);
const ll INF=(1LL<<);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)1e6+; int a[N],b[N],in[N];
ll much[N];
ll mark[N];
void LS(int n)
{
int m=;
for(int i=;i<=n;++i)
b[++m]=a[i];
sort(b+,b++m);
m=unique(b+,b++m)-b-;
for(int i=;i<=n;++i)
a[i]=lower_bound(b+,b++m,a[i])-b;
return;
}
vector<vector<int> >arr(N);
struct node
{
int to,next;
ll val;
}edge[N*];
int head[N];
ll val[N];
bool is[N];
int tot;
void Init(int n)
{
tot=;
for(int i=;i<=n;++i)
val[i]=in[i]=head[i]=is[i]=;
}
void add(int from,int to)
{
++tot;
edge[tot].to=to;
// edge[tot].val=val;
edge[tot].next=head[from];
head[from]=tot;
} queue<int>q;
void bfs(int start)
{
q.push(start);
while(!q.empty())
{
int now=q.front();q.pop();
is[now]=;
for(int i=head[now];i!=;i=edge[i].next)
{
int to=edge[i].to;
in[to]++;
if(!is[to])
{
is[to]=;
q.push(to);
}
}
}
}
void topu(int x)
{
q.push(x);
val[x]=;
while(!q.empty())
{
int now=q.front();q.pop();
for(int i=head[now];i!=;i=edge[i].next)
{
int to=edge[i].to;
in[to]--;
val[to]+=val[now];
if(in[to]==)
q.push(to);
}
}
} void solve()
{
int n;
sc("%d",&n);
Init(n);
for(int i=;i<=n;++i)
{
int J;
sc("%d",&J);
if(J==)
{
int num;
sc("%d",&num);
for(int j=;j<=num;++j)
{
int t;
sc("%d",&t);
arr[i].push_back(t);
}
}
else
{
int u,v;
sc("%d%d",&u,&v);
add(i,u),add(i,v);
}
}
bfs(n);
topu(n);
int num=;
for(int i=;i<=n;++i)
{
if(is[i])
{
int sz=arr[i].size();
for(int j=;j<sz;++j)
a[++num]=arr[i][j],much[num]=val[i];
}
}
LS(num);
for(int i=;i<=num;++i)
mark[i]=;
for(int i=;i<=num;++i)
mark[a[i]]+=much[i];
ll max_=,sum=;
for(int i=;i<=num;++i)
max_=max(max_,mark[i]),sum+=mark[i];
ll ans=;
if(max_>sum-max_)
ans=*(sum-max_);
else
ans=sum;
pr("%lld\n",ans);
for(int i=;i<=n;++i)
arr[i].clear();
} int main()
{
int T;
sc("%d",&T);
while(T--)solve();
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}
Exchanging Gifts--2019CCPC哈尔滨 E题的更多相关文章
- 2017 ccpc哈尔滨 A题 Palindrome
2017 ccpc哈尔滨 A题 Palindrome 题意: 给一个串\(T\),计算存在多少子串S满足\(S[i]=S[2n−i]=S[2n+i−2](1≤i≤n)\) 思路: 很明显这里的回文串长 ...
- 2019ccpc哈尔滨打铜记
小学生日记: 2019.10.13,哈尔滨,打了个铜 开头 先说结论,这次失败,我的锅70%,sdl的锅5%,ykh25% Day0 周五, 我们队出现了奇怪的厄运上身 首先是我中途在飞机上数据线突然 ...
- 2019CCPC秦皇岛 E题 Escape(网络流)
Escape Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- 2019CCPC秦皇岛D题 Decimal
Decimal Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
- 2019CCPC秦皇岛I题 Invoker(DP)
Invoker Time Limit: 15000/12000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- CCPC哈尔滨E题
一堆序列拼接起来,找出现次数大于n/2的数 假设一个数出现次数大于n/2 那么它减去其他数出现的次数一定非负: = c) { cnt += t[i]; } } } } //cout<<c& ...
- 模拟赛小结:The 2019 China Collegiate Programming Contest Harbin Site
比赛链接:传送门 上半场5题,下半场疯狂挂机,然后又是差一题金,万年银首也太难受了. (每次银首都会想起前队友的灵魂拷问:你们队练习的时候进金区的次数多不多啊?) Problem J. Justify ...
- A - Presents
Problem description Little Petya very much likes gifts. Recently he has received a new laptop as a N ...
- The 2019 China Collegiate Programming Contest Harbin Site
题解: https://files.cnblogs.com/files/clrs97/HarbinEditorialV2.zip Code: A. Artful Paintings /* let x= ...
随机推荐
- JavaEE项目开发所需要的包(Struts2+Spring5+Hibernate5)
在这里我只整理了轻量级JavaEE项目开发所需的包 @Auther MrZhangxd 2019-04-29 23:07:21 链接:https://pan.baidu.com/s/16I4KYah ...
- 【Python】使用Beautiful Soup等三种方式定制Jmeter测试脚本
背景介绍 我们在做性能调优时,时常需要根据实际压测的情况,调整线程组的参数,比如循环次数,线程数,所有线程启动的时间等. 如果是在一台Linux机器上,就免不了在本机打开图形页面修改,然后最后传递到压 ...
- 微信小程序之--(与唯品会来场粉红色的邂逅 ???)
Welcome to miaomiaoXiong's segmentfault 微信小程序之--(与唯品会来场粉红色的邂逅 ???) 买买买,虽然双十二刚过,可是唯品会的折扣却是依然火爆.一打开页面, ...
- 风控分类模型种类(决策、排序)比较与模型评估体系(ROC/gini/KS/lift)
python信用评分卡建模(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_ca ...
- 路由传值及获取参数,路由跳转,路由检测,this.$route.query和this.$route.params接收参数,HttpGet请求拼接url参数
配置动态路由参数id: routes: [ // 动态路径参数 以冒号开头 { path: '/user/:id', component: User } ] html路由跳转: <router- ...
- PHP中smarty与MYSQL数据库的连接
进行与MYSQL数据库的关联 先从最简单的数据库查询语句开始 1.先创建mysql数据库 仅仅创建一个测试数据 2.引用smarty框架 3.按照上文在damo文件夹下创建smarty_inc.php ...
- ubuntu tensorflow cpu faster-rcnn 测试自己训练的模型
(flappbird) luo@luo-All-Series:~/MyFile/tf-faster-rcnn_box$ (flappbird) luo@luo-All-Series:~/MyFile/ ...
- xcode报错: 找不到路径或者资源错误:no such file or directory
报错截图: 出现的问题: 运行项目页面图片不显示. 解决方法: 1>[COMMAND+shift+G],前往文件夹,输入: ~/Library/Developer/Xcode/DerivedD ...
- Block的示例学习
@interface ViewController () @property (weak, nonatomic) IBOutlet UIButton *btn; - (IBAction)reset:( ...
- jenkins容器内修改root密码--ubuntu系统
http://www.voidcn.com/article/p-yvnoogkc-ng.html 由于jenkins官方镜像是ubuntu系统,所有啥的都用 sudo 换到root账号,然后登陆har ...