Central Europe Regional Contest 2012 Problem J: Conservation
题目不难,感觉像是一个拓扑排序,要用双端队列来维护;
要注意细节,不然WA到死 = =!
#include<cstdio>
#include<cstring>
#include<vector>
#define maxn 100005
using namespace std; int q[*maxn],count[][maxn],tail,head,n,m,in[maxn];
vector<int>ve[maxn]; int solve(int lab)
{
head=tail=n;
int cnt=,cc=,f=lab;
for(int i=;i<=n;i++)
if(count[-lab][i]==&&in[i]==lab){q[tail++]=i;cnt++;}
if(cnt==)return -;
for(int i=;i<=n;i++)
if(count[-lab][i]==&&in[i]!=lab){q[tail++]=i;}
while(head<tail)
{
int v=q[head++];
if(in[v]!=f){f=in[v];cc++;}
int l=ve[v].size();
for(int i=;i<l;i++)
{
count[-lab][ve[v][i]]--;
if(count[-lab][ve[v][i]]==)
{
if(in[v]==in[ve[v][i]])
q[--head]=ve[v][i];
else q[tail++]=ve[v][i];
}
}
}
return cc;
} int main()
{
int t,x,y;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)ve[i].clear();
memset(count,,sizeof count);
for(int i=;i<=n;i++)scanf("%d",&in[i]);
for(int i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
ve[x].push_back(y);
count[][y]++;
count[][y]++;
}
x=solve();
y=solve();
if(x<||y<)printf("%d\n",max(x,y));
else printf("%d\n",min(x,y));
}
return ;
}
Central Europe Regional Contest 2012 Problem J: Conservation的更多相关文章
- Central Europe Regional Contest 2012 Problem I: The Dragon and the Knights
一个简单的题: 感觉像计算几何,其实并用不到什么计算几何的知识: 方法: 首先对每条边判断一下,看他们能够把平面分成多少份: 然后用边来对点划分集合,首先初始化为一个集合: 最后如果点的集合等于平面的 ...
- Central Europe Regional Contest 2012 Problem c: Chemist’s vows
字符串处理的题目: 学习了一下string类的一些用法: 这个代码花的时间很长,其实可以更加优化: 代码: #include<iostream> #include<string> ...
- Central Europe Regional Contest 2012 Problem H: Darts
http://acm.hunnu.edu.cn/online/problem_pdf/CERC2012/H.pdf HUNNU11377 题意:飞镖环有十个环,没个环从外到里对应一个得分1~10,每个 ...
- ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków
ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik’s Rect ...
- 2020.5.16-ICPC Central Europe Regional Contest 2019
A. ABB #include <bits/stdc++.h> using namespace std; #define PB push_back #define ZERO (1e-10) ...
- 2017-2018 ACM-ICPC, Central Europe Regional Contest (CERC 17)
A. Assignment Algorithm 按题意模拟即可. #include<stdio.h> #include<iostream> #include<string ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem J
Problem J Joking with Fermat's Last Theorem Fermat's Last Theorem: no three positive integers a, b, ...
- 2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/100 ...
- 【枚举】Southwestern Europe Regional Contest H - Sheldon Numbers
https://vjudge.net/contest/174235#problem/H [题意] 求[x,y]之间有多少个Sheldon Number Sheldon Number是二进制满足以下条件 ...
随机推荐
- 两种JSON数据类型的解析
son数据格式解析我自己分为两种: 一种是普通的,一种是带有数组形式的: 普通形式的:服务器端返回的json数据格式如下: {"userbean":{"Uid" ...
- Objective-C:runtime
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...
- Android_layout_note
LinearLayout线程布局 LinearLayout属性 android:orientation表示线性布局的方向 vertical: 垂直.从上往下 horizontal: 水平.从左往右 a ...
- Android开发之ViewPager
什么是ViewPager? ViewPager是安卓3.0之后提供的新特性,继承自ViewGroup,专门用以实现左右滑动切换View的效果. 如果想向下兼容就必须要android-support-v ...
- SqlServer2008误操作数据(delete或者update)后恢复数据
实际工作中,有时会直接在数据库中操作数据,比如对数据进行delete或者update操作,当进行这些操作的时候,如果没有加上where条件或者where条件不合理,那么导致的结果可想而知,如果操作的又 ...
- MVC小系列(二十)【给Action提供HttpStatusCodeResult】
主要用到: HttpStatusCodeResult 和HttpStatusCode 的http返回状态 比如: /// <summary> /// 使用异步模式 /// </sum ...
- cmd命令积累
dir:展示所有目录 cd fileName:进入下一个目录 cd .. :返回上一层目录 cd\:返回根目录
- primefaces 带参数的组件
分析了一下primefaces extension showcase的源码,总结一下组件传参的方式. 下面定义了一个组件,名为fourTabsDecoratorCustom.xhtml 1: < ...
- HTTP协议认识
一.HTTP协议概念 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是用于从WWW服务器传输超文本到本地浏览器的传送协议 HTTP是一个应用层协议,由请求和响应 ...
- 查内网虚拟机映射的公网IP
1.访问ip138.com 2.curl ifconfig.me