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= ...
随机推荐
- Java面向对象4(P~U)
P 3-1 Point类的构造函数 (SDUT 2670) import java.util.Arrays; import java.util.Scanner; public class Mai ...
- 8月清北学堂培训 Day5
今天是杨思祺老师的讲授~ 最短路练习题: POJ 1125 Stockbroker Grapevine 有 N 个股票经济人可以互相传递消息,他们之间存在一些单向的通信路径.现在有一个消息要由某个人开 ...
- 解决ScrollView中Recyclerview显示不全,滑动不流畅的问题
这个问题经常会碰到,看了下网上关于这个问题的解决方案,有很多都是复制粘贴的,并不能根本解决问题 比较有效的一种方案是在Recyclerview的外层套一个RelativeLayout 然后设置recy ...
- super关键字和调用父类构造方法
表示父类对象的默认引用 如果子类要调用父类被覆盖的实例方法,可用super作为调用者调用父类被覆盖的实例方法. 使用super调用父类方法 使用super调用父类的构造方法 调用构造方法 本类中调用另 ...
- windows下安装mongodb数据库以及使用数据库
首先下载mongodb, 链接: https://pan.baidu.com/s/1KyvF7bAqGM8K-ir-hFNhPw 密码: vlc9 双击进行安装 勾选我接受并单击next 选择cust ...
- 表单 Flask-WTF - 校验器
1 wtforms内置的校验器 Class wtforms.validators.DataRequired(message=None)此验证器将会检测field是否输入了数值,实际上是进行了if fi ...
- Protocol Buffers学习笔记
Protocol Buffers学习笔记 1. 简介 Protocol Buffers是google发明的一种数据交换格式,独立于语言,独立于平台.与其他的数据交换格式有所不同,Protocol Bu ...
- BGP多线 有些BGP IP也会被极少数运营商劫持 取Ip逻辑
小结: 1.租用的服务器只有一个IP,用户的访问路线是由路由器根据访客的实际访问速度选择最优访问路径,来选择访问的.而且不占用任何的服务器资源.服务器的上行和下行都是有路由器来选择最佳的路线,所以这样 ...
- [Java读书笔记] Effective Java(Third Edition) 第 7 章 Lambda和Stream
在Java 8中,添加了函数式接口(functional interface),Lambda表达式和方法引用(method reference),使得创建函数对象(function object)变得 ...
- Docker 官方安装详解
# 0x00 安装热身 - 针对CE版本 Docker 分为 CE 和 EE 版本, CE免费,EE收费,CE版以及能够满足我们所有需求 本文针对 CE 版本的安装进行说明 - 系统说明 本文以 Ce ...