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= ...
随机推荐
- Density of Power Network(ZOJ 3708)
Problem The vast power system is the most complicated man-made system and the greatest engineering i ...
- vue的一些随笔
一.点击路由后的样式,可以在路由文件index.js中设置 再在样式里面设置active的类名对应的样式. ———————————————————————————————————————————— 二 ...
- hive 常用参数
hive.exec.max.created.files •说明:所有hive运行的map与reduce任务可以产生的文件的和 •默认值:100000 hive.exec.dynamic.partit ...
- Nginx中配置非英文域名
前两天遇到个配置越南语的域名的情况.域名和ip解析完成后,直接ping域名也不通,还以为是解析问题.研究了半天,nginx配置非英文域名时,需要有其他操作. 非英文域名转换成punycode编码才可以 ...
- Windows下使用cmd运行jar文件
一般window系统下是不能直接运行jar文件的.(有些电脑可以,记得我以前的电脑是双击jar就可以运行的) 那么如何在windows下运行jar呢? 1.首先,电脑必须配置java运行环境jre-- ...
- selenium 配置 chromedriver
参考文档: https://blog.csdn.net/yoyocat915/article/details/80580066?tdsourcetag=s_pcqq_aiomsg http://npm ...
- Mysql触发器详解以及union的使用
---恢复内容开始--- Mysql触发器定义: 当一个表中有insert update delete事件发生,触发一个事件,执行一段代码.作用: 同步数据创建: create trigger 名称 ...
- docker swarm和 k8s对比
Swarm的优势:swarm API兼容docker API,使得swarm 学习成本低,同时架构简单,部署运维成本较低.Swarm的劣势:同样是因为API兼容,无法提供集群的更加精细的管理.在网络方 ...
- OpenCL如何判定一个work-group的最大Local Memory大小
最近有不少朋友提及到如何能在运行时获悉一个GPU的最大local memory的尺寸.由于OpenCL对各类处理器开放,因此不同处理器所拥有的local memory大小也各不相同.即便是GPU,甚至 ...
- 七天学会ASP.NET MVC
地址一: http://www.cnblogs.com/powertoolsteam/p/MVC_one.html http://www.cnblogs.com/powertoolsteam/p/MV ...