【atcoder F - Namori】**
F- Namori
http://agc004.contest.atcoder.jp/tasks/agc004_f
Time limit : 2sec / Memory limit : 256MB
Score : 2200 points
Problem Statement
You are given an undirected graph with N vertices and M edges. Here, N−1≤M≤N holds and the graph is connected. There are no self-loops or multiple edges in this graph.
The vertices are numbered 1 through N, and the edges are numbered 1 through M. Edge i connects vertices ai and bi.
The color of each vertex can be either white or black. Initially, all the vertices are white. Snuke is trying to turn all the vertices black by performing the following operation some number of times:
- Select a pair of adjacent vertices with the same color, and invert the colors of those vertices. That is, if the vertices are both white, then turn them black, and vice versa.
Determine if it is possible to turn all the vertices black. If the answer is positive, find the minimum number of times the operation needs to be performed in order to achieve the objective.
Constraints
- 2≤N≤105
- N−1≤M≤N
- 1≤ai,bi≤N
- There are no self-loops or multiple edges in the given graph.
- The given graph is connected.
Partial Score
- In the test set worth 1500 points, M=N−1.
Input
The input is given from Standard Input in the following format:
NMa1b1a2b2:aMbMOutput
If it is possible to turn all the vertices black, print the minimum number of times the operation needs to be performed in order to achieve the objective. Otherwise, print
-1instead.
Sample Input 1
Copy6 5
1 2
1 3
1 4
2 5
2 6Sample Output 1
Copy5For example, it is possible to perform the operations as shown in the following diagram:
Sample Input 2
Copy3 2
1 2
2 3Sample Output 2
Copy-1It is not possible to turn all the vertices black.
Sample Input 3
Copy4 4
1 2
2 3
3 4
4 1Sample Output 3
Copy2This case is not included in the test set for the partial score.
Sample Input 4
Copy6 6
1 2
2 3
3 1
1 4
1 5
1 6Sample Output 4
Copy7This case is not included in the test set for the partial score.
【分析】
这题真神!!
如果是一棵树的话。
树是二分图,所以我们将他黑白染色,问题变成了,可以交换相邻的黑色和白色,使得最后图黑白倒置。
把白色看成空格,黑色看成棋子,就是树上有x个棋子,你可以往空格里面移动,问你最少多少步到达目标状态。
在树上,其实方案是唯一的,你求一下子树里面现在有多少个棋子,目标是多少个棋子,你就知道一定会从父亲那里运过来多少棋子(或者从这个运向父亲多少个棋子)
这个是直接求就可以了的,就是$\sum ai-bi$
黑点个数初末态不同则无解。
当有环怎么办?
我们分类讨论:
1、构成奇环,多了的一条边连向的两点是同色的,就是说两个点那里可以同时加2个黑点或者同时减两个黑点,加/减多少个黑点你是知道的,(黑点奇偶初末态不同则无解),你就直接把那些黑点加上去,然后做法跟前面一样了。
2、构成偶环,就是说多了的那条边也可以运棋子,假设这条边向上运了x个棋子,然后就也是树上的问题。你的ans最后会写成|Ai-x|或|Ai+x|或|Ai|的形式,这种形式的和很经典啦,就是数轴上的距离和,我们x取其中的中位数就能算出最优解。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define Maxn 100010
#define LL long long
const int INF=; int mymin(int x,int y) {return x<y?x:y;} struct node
{
int x,y,next;
}t[Maxn*];
int len,first[Maxn]; void ins(int x,int y)
{
t[++len].x=x;t[len].y=y;
t[len].next=first[x];first[x]=len;
} int r1,r2,ad;
int sm[Maxn],ss[Maxn],ans,d[Maxn],h[Maxn];
int dfs(int x,int fa,int dep)
{
sm[x]=;ss[x]=dep;d[x]=dep;
if(x==r1||x==r2) sm[x]+=ad,ss[x]+=ad;
int tt=;
for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa)
{
int y=t[i].y;
tt+=dfs(y,x,-dep);
sm[x]+=sm[y];ss[x]+=ss[y];
}
if(x==r1) tt++;
if(x==r2) tt--;
if(tt==-) h[++h[]]=ss[x]-(sm[x]-ss[x]);
else if(tt==) h[++h[]]=(sm[x]-ss[x])-ss[x];
else ans+=abs(ss[x]-(sm[x]-ss[x]));
// ans+=abs(ss[x]-(sm[x]-ss[x]));
return tt;
} int ff[Maxn];
int ffa(int x)
{
if(ff[x]!=x) ff[x]=ffa(ff[x]);
return ff[x];
} int main()
{
// int T;
// scanf("%d",&T);
// while(T--)
{
int n,m;r1=r2=;
scanf("%d%d",&n,&m);
len=;
memset(first,,sizeof(first));
for(int i=;i<=n;i++) ff[i]=i;
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
if(ffa(x)!=ffa(y))
{
ff[ffa(x)]=ffa(y);
ins(x,y);ins(y,x);
}
else r1=x,r2=y;
}
if(m==n-)
{
dfs(,,);
ans=;
for(int i=;i<=n;i++) ans+=abs(ss[i]-(sm[i]-ss[i]));
if(sm[]-ss[]!=ss[]) printf("-1\n");
else printf("%d\n",ans);
}
else
{
// printf("-2\n");
ad=;
dfs(,,);
if(d[r1]==d[r2])
{
ad=abs(ss[]-(sm[]-ss[]));
if(ad&) printf("-1\n");
else
{
ad/=;
if(ss[]>sm[]-ss[]) dfs(,,);
else dfs(,,);
ans=ad;
for(int i=;i<=n;i++) ans+=abs(ss[i]-(sm[i]-ss[i]));
printf("%d\n",ans);
}
}
else
{
h[]=;h[++h[]]=;
ans=;
dfs(,,);
if(sm[]-ss[]!=ss[]) printf("-1\n");
else
{
sort(h+,h++h[]);
int x=h[h[]/+];
for(int i=;i<=h[];i++) ans+=abs(h[i]-x);
printf("%d\n",ans);
}
}
}
}
return ;
}
【代码有点丑。。
2017-04-19 09:43:00
【atcoder F - Namori】**的更多相关文章
- 【Atcoder yahoo-procon2019-qual D】 Ears
Atcoder yahoo-procon2019-qual D 题意:给你\(L\)个耳朵(???),以及一条范围从\(0\)到\(L\)的数轴,你可以选择一个出发点,从该点开始随意走动,如果经过了\ ...
- 【AtCoder】ARC067 F - Yakiniku Restaurants 单调栈+矩阵差分
[题目]F - Yakiniku Restaurants [题意]给定n和m,有n个饭店和m张票,给出Ai表示从饭店i到i+1的距离,给出矩阵B(i,j)表示在第i家饭店使用票j的收益,求任选起点和终 ...
- 【AtCoder Regular Contest 082 F】Sandglass
[链接]点击打开链接 [题意] 你有一个沙漏. 沙漏里面总共有X单位的沙子. 沙漏分A,B上下两个部分. 沙漏从上半部分漏沙子到下半部分. 每个时间单位漏1单位的沙子. 一开始A部分在上面.然后在r1 ...
- 【Wannafly挑战赛4】F 线路规划 倍增+Kruskal+归并
[Wannafly挑战赛4]F 线路规划 题目描述 Q国的监察院是一个神秘的组织.这个组织掌握了整个帝国的地下力量,监察着Q国的每一个人.监察院一共有N个成员,每一个成员都有且仅有1个直接上司,而他只 ...
- 【AtCoder Regular Contest 082 A】Together
[链接]点击打开链接 [题意] 给你n个数字,每个位置上的数字可以+1,不变,或-1,每个位置只能操作一次. 操作完之后,让你选一个数字x,然后统计a[i]==x的个数count. 问你count的最 ...
- 【AtCoder Regular Contest 082】Derangement
[链接]点击打开链接 [题意] 在这里写题意 [题解] 贪心. 连续一块的p[i]==i的话,对答案的贡献就应该为(这个连续块的长度+1)/2; 长度为1的也正确. (也即两两相邻的互换位置.) [错 ...
- 【AtCoder Beginner Contest 074 D】Restoring Road Network
[链接]h在这里写链接 [题意] 给你任意两点之间的最短路. 让你求出原图. 或者输出原图不存在. 输出原图的边长总和的最小值. [题解] floyd算法. 先在原有的矩阵上. 做一遍floyd. 如 ...
- 【AtCoder Beginner Contest 074 C】Sugar Water
[链接]h在这里写链接 [题意] 让你在杯子里加糖或加水. (4种操作类型) 糖或水之间有一定关系. 糖和水的总量也有限制. 问你糖水浓度的最大时,糖和糖水的量. [题解] 写个dfs就好. 每次有4 ...
- 【AtCoder Beginner Contest 074 B】Collecting Balls (Easy Version)
[链接]h在这里写链接 [题意] 看懂题目之后就会发现是道大水题. [题解] 在这里写题解 [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc++.h&g ...
随机推荐
- 关于Re模块的一些基础知识(另附一段批量抓代理ip的代码)
1.常用匹配规则 . 表示任意字符[0-9] 用来匹配一个指定的字符类别[^5]表示除了5之外的其他字符,^不在字符串的开头,则表示它本身.* 对于前一个字符重复0到无穷次+ 对于前一个字符重复1到无 ...
- php webshell常见函数
0x1 直接在字符串变量后面加括号, 会调用这个函数: <?php $s = 'system'; $e = 'assert'; $s('whoami'); $e('phpinfo();'); 0 ...
- Thinkphp的自定义路由(route.php)
废话:因为thinkphp的默认路由会导致URL特别长,从而会影响搜索引擎优化.所以就衍生了自定义路由,尽量将URL缩短. 这是默认的路由文件: <?php return [ '__patter ...
- nginx与PHP的关系和交互方式【转】
nginx与PHP的关系. 对比, apache和PHP的关系, 将PHP安装成apache的一个功能模块, 导致的结果, 对外只有一个apache程序, PHP并不独立出现, 仅仅是apache的模 ...
- Elasticsearch5.0 安装问题集锦【转】
转自 Elasticsearch5.0 安装问题集锦 - 代码&优雅着&生活 - 博客园http://www.cnblogs.com/sloveling/p/elasticsearch ...
- centos7-sar工具的安装过程及其简单应用
一.sar工具安装 1.进入yum配置文件目录: cd /etc/yum.repos.d/ 2.vi CentOS-Base.repo命令创建文件CentOS-Base.repo 文件内容见网页:ht ...
- C中常用格式格式码
一.常用printf格式码 二.常用scanf格式码
- Groovy 与 DSL
一:DSL 概念 指的是用于一个特定领域的语言(功能领域.业务领域).在这个给出的概念中有 3个重点: 只用于一个特定领域,而非所有通用领域,比如 Java / C++就是用于通用领域,而不可被称为 ...
- Eloquent中一些其他的create方法
firstOrCreate/ firstOrNew# 还有两种其它方法,你可以用来通过属性批量赋值创建你的模型:firstOrCreate 和firstOrNew.firstOrCreate 方法将会 ...
- 响应式设计:根据不同设备引不同css样式
<link rel="stylesheet" media="screen and (max-width:600px)" href="small. ...



