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≤MN 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≤MN
  • 1≤ai,biN
  • 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:aMbM

Output

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 -1 instead.


Sample Input 1

Copy
6 5
1 2
1 3
1 4
2 5
2 6

Sample Output 1

Copy
5

For example, it is possible to perform the operations as shown in the following diagram:


Sample Input 2

Copy
3 2
1 2
2 3

Sample Output 2

Copy
-1

It is not possible to turn all the vertices black.


Sample Input 3

Copy
4 4
1 2
2 3
3 4
4 1

Sample Output 3

Copy
2

This case is not included in the test set for the partial score.


Sample Input 4

Copy
6 6
1 2
2 3
3 1
1 4
1 5
1 6

Sample Output 4

Copy
7

This 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】**的更多相关文章

  1. 【Atcoder yahoo-procon2019-qual D】 Ears

    Atcoder yahoo-procon2019-qual D 题意:给你\(L\)个耳朵(???),以及一条范围从\(0\)到\(L\)的数轴,你可以选择一个出发点,从该点开始随意走动,如果经过了\ ...

  2. 【AtCoder】ARC067 F - Yakiniku Restaurants 单调栈+矩阵差分

    [题目]F - Yakiniku Restaurants [题意]给定n和m,有n个饭店和m张票,给出Ai表示从饭店i到i+1的距离,给出矩阵B(i,j)表示在第i家饭店使用票j的收益,求任选起点和终 ...

  3. 【AtCoder Regular Contest 082 F】Sandglass

    [链接]点击打开链接 [题意] 你有一个沙漏. 沙漏里面总共有X单位的沙子. 沙漏分A,B上下两个部分. 沙漏从上半部分漏沙子到下半部分. 每个时间单位漏1单位的沙子. 一开始A部分在上面.然后在r1 ...

  4. 【Wannafly挑战赛4】F 线路规划 倍增+Kruskal+归并

    [Wannafly挑战赛4]F 线路规划 题目描述 Q国的监察院是一个神秘的组织.这个组织掌握了整个帝国的地下力量,监察着Q国的每一个人.监察院一共有N个成员,每一个成员都有且仅有1个直接上司,而他只 ...

  5. 【AtCoder Regular Contest 082 A】Together

    [链接]点击打开链接 [题意] 给你n个数字,每个位置上的数字可以+1,不变,或-1,每个位置只能操作一次. 操作完之后,让你选一个数字x,然后统计a[i]==x的个数count. 问你count的最 ...

  6. 【AtCoder Regular Contest 082】Derangement

    [链接]点击打开链接 [题意] 在这里写题意 [题解] 贪心. 连续一块的p[i]==i的话,对答案的贡献就应该为(这个连续块的长度+1)/2; 长度为1的也正确. (也即两两相邻的互换位置.) [错 ...

  7. 【AtCoder Beginner Contest 074 D】Restoring Road Network

    [链接]h在这里写链接 [题意] 给你任意两点之间的最短路. 让你求出原图. 或者输出原图不存在. 输出原图的边长总和的最小值. [题解] floyd算法. 先在原有的矩阵上. 做一遍floyd. 如 ...

  8. 【AtCoder Beginner Contest 074 C】Sugar Water

    [链接]h在这里写链接 [题意] 让你在杯子里加糖或加水. (4种操作类型) 糖或水之间有一定关系. 糖和水的总量也有限制. 问你糖水浓度的最大时,糖和糖水的量. [题解] 写个dfs就好. 每次有4 ...

  9. 【AtCoder Beginner Contest 074 B】Collecting Balls (Easy Version)

    [链接]h在这里写链接 [题意] 看懂题目之后就会发现是道大水题. [题解] 在这里写题解 [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc++.h&g ...

随机推荐

  1. ① 设计模式的艺术-07.适配器(Adapter)模式

    什么是适配器模式? 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 模式中的角色 目标接口(Target):客户所期待的接口 ...

  2. GridControl详解(三)列数据的格式设置

    为了测试方便,我们加入新的3列,格式分别是数据,时间,字符串.代码增加下列部分: //格式增加 dt.Columns.Add("数据",typeof(decimal)); dt.C ...

  3. 利用PhantomJS生成网站截图

    var page = require('webpage').create(); page.open('http://qq.com', function () { page.render('exampl ...

  4. 5、Linux操作系统介绍

    1操作系统的作用·是现代计算机系统中最基本和最重要的系统软件·是配置在计算机硬件上的第一层软件,是对硬件系统的首次扩展·主要作用是管理好硬件设备,并为用户和应用程序提供一个简单的接口,以便于使用·而其 ...

  5. 重载jquery on方法实现click事件在移动端的快速响应

    额,这个标题取的还真是挺装的... 其实我想表达的是jquery click事件如何在移动端自动转换成touchstart事件. 因为移动端click事件会比touchstart事件慢几拍 移动设备某 ...

  6. 网易android开发面试题及心得

    前几天面试网易android开发,总体感觉问题难度一般.怪我自己没有好好梳理知识,尤其是基础,后面就没消息了... 笔试: 1.描述Activity 生命周期 2.什么是ANR,如何规避? 3.描述a ...

  7. 深入理解Spring系列之八:常用的扩展接口

    转载 https://mp.weixin.qq.com/s/XfhZltSlTall8wKwV_7fKg Spring不仅提供了一个进行快速开发的基础框架,而且还提供了很多可扩展的接口,用于满足一些额 ...

  8. 【IDEA】IDEA中maven项目pom.xml依赖不生效解决

    问题: 今天在web项目中需要引入poi相关jar包.查看之下才发现pom.xml中的依赖虽然已经下载到了本地仓库 repository,但是却没有加入到项目路径的 Extenal Libraries ...

  9. Tomcat 调优及 JVM 参数优化

    Tomcat 本身与 JVM 优化 Tomcat:调整Server.xml JVM:bat启动服务方式的话修改catalina.bat 服务式启动的话参考:http://www.cnblogs.com ...

  10. C#技术分享【PDF转换成图片——11种方案】

    1.[iTextSharp.dll],C# 开源PDF处理工具,可以任意操作PDF,并可以提取PDF中的文字和图片,但不能直接将PDF转换成图片. DLL和源码 下载地址:http://downloa ...