AtCoder - 4162 Independence
Problem Statement
In the State of Takahashi in AtCoderian Federation, there are N cities, numbered 1,2,…,N. M bidirectional roads connect these cities. The i-th road connects City Ai and City Bi. Every road connects two distinct cities. Also, for any two cities, there is at most one road that directly connects them.
One day, it was decided that the State of Takahashi would be divided into two states, Taka and Hashi. After the division, each city in Takahashi would belong to either Taka or Hashi. It is acceptable for all the cities to belong Taka, or for all the cities to belong Hashi. Here, the following condition should be satisfied:
- Any two cities in the same state, Taka or Hashi, are directly connected by a road.
Find the minimum possible number of roads whose endpoint cities belong to the same state. If it is impossible to divide the cities into Taka and Hashi so that the condition is satisfied, print -1
.
Constraints
- 2≤N≤700
- 0≤M≤N(N−1)⁄2
- 1≤Ai≤N
- 1≤Bi≤N
- Ai≠Bi
- If i≠j, at least one of the following holds: Ai≠Aj and Bi≠Bj.
- If i≠j, at least one of the following holds: Ai≠Bj and Bi≠Aj.
Input
Input is given from Standard Input in the following format:
N M
A1 B1
A2 B2
:
AM BM
Output
Print the answer.
Sample Input 1
5 5
1 2
1 3
3 4
3 5
4 5
Sample Output 1
4
For example, if the cities 1,2 belong to Taka and the cities 3,4,5 belong to Hashi, the condition is satisfied. Here, the number of roads whose endpoint cities belong to the same state, is 4.
Sample Input 2
5 1
1 2
Sample Output 2
-1
In this sample, the condition cannot be satisfied regardless of which cities belong to each state.
Sample Input 3
4 3
1 2
1 3
2 3
Sample Output 3
3
Sample Input 4
10 39
7 2
7 1
5 6
5 8
9 10
2 8
8 7
3 10
10 1
8 10
2 3
7 4
3 9
4 10
3 4
6 1
6 7
9 5
9 7
6 9
9 4
4 6
7 5
8 3
2 5
9 2
10 7
8 6
8 9
7 3
5 3
4 5
6 3
2 10
5 10
4 2
6 2
8 4
10 6
Sample Output 4
21 打ARC的时候都想到模型了。。。但就是没做出来mmp,还是水平差啊QWQ
主要没想到的地方是: 如果把补图二分图染色(不是二分图就无解),每个联通分量挑一类点出来弄到一起一定能凑成最后的一个团。
我也不知道为什么当时没想到QWQ,明明这么简单。。。。
然后直接背包完了更新答案就好了QWQ,怎么看都是一个NOIP题,药丸药丸
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=705; inline int Get(int x){ return x*(x-1)>>1;} int num[3],n,m,ans=1<<30,uu,vv,col[N],now;
bool f[N][N],g[N][N]; bool dfs(int x,int c){
col[x]=c,num[c]++; for(int i=1;i<=n;i++) if(!g[x][i])
if(!col[i]){ if(!dfs(i,3-c)) return 0;}
else if(col[i]==c) return 0; return 1;
} int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
scanf("%d%d",&uu,&vv),g[uu][vv]=g[vv][uu]=1; f[0][0]=1;
for(int i=1;i<=n;i++) g[i][i]=1; for(int i=1;i<=n;i++) if(!col[i]){
num[1]=num[2]=0;
if(!dfs(i,1)){ puts("-1"); return 0;} now++;
for(int j=0;j<=n;j++) if(f[now-1][j]) f[now][j+num[1]]=f[now][j+num[2]]=1;
} for(int i=0;i<=n;i++) if(f[now][i]) ans=min(ans,Get(i)+Get(n-i)); printf("%d\n",ans);
return 0;
}
AtCoder - 4162 Independence的更多相关文章
- AtCoder Regular Contest 099 (ARC099) E - Independence 二分图
原文链接https://www.cnblogs.com/zhouzhendong/p/9224878.html 题目传送门 - ARC099 E - Independence 题意 给定一个有 $n$ ...
- AtCoder Regular Contest 099
AtCoder Regular Contest 099 C - Minimization 题意 题意:给出一个n的排列.每次操作可以使一段长度为K的连续子序列变成该序列的最小数.求最少几次使得整个数列 ...
- AtCoder Beginner Contest 084 C - Special Trains
Special Trains Problem Statement A railroad running from west to east in Atcoder Kingdom is now comp ...
- HDU 4162 最小表示法
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4162 题意:给定一个只有0-7数字组成的串.现在要由原串构造出一个新串,新串的构造方法:相邻2个位置的数字 ...
- 控制反转(IOC: Inverse Of Control) & 依赖注入(DI: Independence Inject)
举例:在每天的日常生活中,我们离不开水,电,气.在城市化之前,我们每家每户需要自己去搞定这些东西:自己挖水井取水,自己点煤油灯照明,自己上山砍柴做饭.而城市化之后,人们从这些琐事中解放了出来,城市中出 ...
- AtCoder Regular Contest 061
AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...
- Independence独立
Independence refers to the degree to which each test case stands alone. That is, does the success or ...
- HDU 4162 Shape Number
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4162 题意: 求给定字符的一阶差分链的最小表示. 题解: 先求一阶差分链,再求一阶差分链的最小表示法 ...
- AtCoder Grand Contest 001 C Shorten Diameter 树的直径知识
链接:http://agc001.contest.atcoder.jp/tasks/agc001_c 题解(官方): We use the following well-known fact abou ...
随机推荐
- VMware 12安装虚拟机Mac OS X 10.10不能上网问题
1:从本机中选择打开连接网络,选择本地连接.如果是无线网可以选择无线网. 1: 2: 控制面板--->网络和共享中心 2:选择属性,点击共享按钮. 3:将internet连接共享下面选项都选中 ...
- 顺序图(Sequence Diagram)
顺序图(Sequence Diagram): 是一种强调对象间消息传递次序的交互图,又称为时序图或序列图.描述了在一个用例或操作的执行过程中对象如何通过消息相互交互,说明了消息如何在对象之间被发送和接 ...
- 4.0docker部署
设置容器的端口映射 -P :容器暴露的所有端口映射 -p :指定映射容器暴露的端口 Nginx部暑流程 docker run -p 80 --name web -t -i ubuntu /bin/b ...
- vs 2015 插件 supercharger 破解方式
亲测有效:效果如图 方法如下: 1.打开Supercharger的options; 2.点击Pricing & Registration 3.复制 license 然后再按Paste &am ...
- thread_info&内核栈
转载:http://blog.chinaunix.net/uid-22548820-id-2125152.html 之所以将thread_info结构称之为小型的进程描述符,是因为在这个结构中并没有直 ...
- Linux实际用户(组)ID,有效用户(组)ID,设置用户(组)ID
实际用户(组)ID: 标识用户是谁,这两个字段在登录时取自口令文件中的登录项. 有效用户(组)ID: 决定了对文件的访问权限,通常有效用户(组)ID等于实际用户(组)ID,谁运行有效ID就等于谁的实际 ...
- MediaWiki安装配置(Linux)【转】
转自:http://blog.csdn.net/gao36951/article/details/43965527 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 1Media ...
- python基础===python基础知识问答(转)
1.到底什么是Python?你可以在回答中与其他技术进行对比 Python是一种解释型语言.与C语言和C的衍生语言不同,Python代码在运行之前不需要编译.其他解释型语言还包括PHP和Ruby. P ...
- linux内核启动分析(3)
主要分析do_basic_setup函数里面的do_initcalls()函数,这个函数用来调用所有编译内核的驱动模块中的初始化函数. static void __init do_initcalls( ...
- 【模板】BZOJ 3781: 小B的询问 莫队算法
http://www.lydsy.com/JudgeOnline/problem.php?id=3781 N个数的序列,每次询问区间中每种数字出现次数的平方和,可以离线. 丢模板: #include ...