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. 漫谈JWT

    一.JWT简介[对于了解JWT的童鞋,可以直接跳到最后] 咱们就不弄那些乱七八糟的概念,就简单点说一下JWT是什么.有什么和能干什么 1. JWT概念和作用 JWT全称为json web token, ...

  2. 2008 Round 1A C Numbers (矩阵快速幂)

    题目描述: 请输出(3+√5)^n整数部分最后3位.如果结果不超过2位,请补足前导0. 分析: 我们最容易想到的方法肯定是直接计算这个表达式的值,但是这样的精度是不够的.朴素的算法没有办法得到答案.但 ...

  3. python小爬虫练手

    一个人无聊,写了个小爬虫爬取不可描述图片.... 代码太短,就暂时先往这里贴一下做备份吧. 注:这是很严肃的技术研究,当然爬下来的图片我会带着批判性的眼光审查一遍的....   :) #! /usr/ ...

  4. 深入理解Spring系列之七:web应用自动装配Spring配置

    转载 https://mp.weixin.qq.com/s/Lf4akWFmcyn9ZVGUYNi0Lw 在<深入理解Spring系列之一:开篇>的示例代码中使用如下方式去加载Spring ...

  5. linux 查看内存和cpu占用比较多的进程

    1.可以使用一下命令查使用内存最多的10个进程        ps -aux | sort -k4nr | head -n 102. 可以使用一下命令查使用CPU最多的10个进程        ps ...

  6. Python3 动态导入模块的两种方式

    动态导入模块就是只知道str类型的模块名字符串,通过这个字符串导入模块 需要导入的模块: #!/usr/bin/env python # _*_ coding:utf-8 _*_ # Author:C ...

  7. BZOJ - Problem 3622 - 已经没有什么好害怕的了

    题意: 给定两个序列$a$和$b$,让它们进行匹配,求出使得$a_i > b_j$的个数比$a_i < b_j$的个数恰好多$k$,求这样的匹配方法数 题解: 这题的各种表示有一点相似又截 ...

  8. gdb安装

    1.卸载原有gdb  以root用户登录  1.1 查询原有gdb包名,执行命令: rpm -q gdb  1.2 卸载原有gdb包,假设gdb包名为gdb-7.0-0.4.16,执行命令:rpm - ...

  9. PHP 利用nginx的X-sendfile控制下载,提高下载效率

    https://blog.csdn.net/qq_34839657/article/details/52812885 https://www.jianshu.com/p/bf5c387830b7 为了 ...

  10. 你需要知道的Nginx配置二三事

    做服务端开发的,工作中难免会遇到处理Nginx配置相关问题.在配置Nginx时,我一直本着“照葫芦画瓢”的原则,复制已有的配置代码,自己修修改改然后完成配置需求,当有人问起Nginx相关问题时,其实仍 ...