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= ...
随机推荐
- 将省市县三级联动的json数据,转化为element-ui能用的格式,并使用
var options=[]; var cities = { '北京': { '北京': ['东城区', '西城区', '崇文区', '宣武区', '朝阳区', '丰台区', '石景山区', '海淀区 ...
- JavaWeb_(Mybatis框架)MyBatis整合Spring框架
MyBatis + Spring整合开发 a)使用Spring容器用单例模式管理Mybatis的sqlSessionFactory:b)使用Spring管理连接池.数据源等:c)将Dao/Mapper ...
- open suse tumbleweed安装记录
zypper install imagewriter cmake blender fontforge gimp digikam inkscape kontact pitivi smplayer si ...
- Java 类加载
类的生命周期 类的生命周期是从class文件到类加载 开始.类卸载结束. https://gitbook.cn/books/5cb844a55f75cc777e4c2320/index.html
- CISCO实验记录六:EIGRP路由协议
一.要求 1.查看当前路由协议 2.清空路由设置 3.使用EIGRP协议创建路由 4.查看EIGRP的邻居表 5.关闭自动汇总 6.使用手工汇总 二.实现 1.查看当前路由协议 #show ip pr ...
- 重读APUE(9)-SIG_ERR、SIG_DFL、SIG_IGN定义无参数
下面这几个函数定义,每次看到都会纠结一阵子,奇怪的是为什么没有参数? #define SIG_ERR (void (*)())-1 #define SIG_DFL (void (*)())0 #def ...
- RabbitMQ JAVA客户端调用例子
1.安装erlang 下载地址:http://www.erlang.org/downloads 设置ERLANG环境变量 2.安装RabbitMQ 下载地址: http://www.rabbitmq. ...
- mac下iterm配色、半透明与样式设置
mac下iterm配色.半透明与样式设置 * {display: table-row!important} .MJXp-surd {vertical-align: top} .MJXp-surd &g ...
- Http的请求协议请求行介绍
请求协议包含的内容 请求行 GET /day04-tomcat/index.jsp HTTP/1.1 HTTP/1.1: 表示的是我们使用的是http协议的1.1版本 请求头 请求空行 请求体: 存储 ...
- Hadoop 部署之 ZooKeeper (二)
目录 一.Zookeeper功能简介 二.ZooKeeper基本概念 1.集群角色 三.ZooKeeper 的安装 1.下载安装(在datanode节点安装) 2.配置ZooKeeper环境变量 3. ...