SPOJ UOFTCG - Office Mates (树的最小路径覆盖)
UOFTCG - Office Mates
Dr. Baws has an interesting problem. His N
graduate students, while friendly with some select people, are
generally not friendly with each other. No graduate student is willing
to sit beside a person they aren't friends with.
The desks are up against the wall, in a single line, so it's possible
that Dr. Baws will have to leave some desks empty. He does know which
students are friends, and fortunately the list is not so long: it turns
out that for any subset of K
graduate students, there are at most K−1
pairs of friends. Dr. Baws would like you to minimize the total number of desks required. What is this minimum number?
Input
The input begins with an integer T≤50
, the number of test cases. Each test case begins with two integers on their own line: N≤100000, the number of graduate students (who are indexed by the integers 1 through N), and M, the number of friendships among the students. Following this are M lines, each containing two integers i and j separated by a single space. Two integers i and j represent a mutual friendship between students i and j
.
The total size of the input file does not exceed 2 MB.
Output
For each test case output a single number: the minimum number of desks Dr. Baws requires to seat the students.
Example
Input:
1
6 5
1 2
1 3
1 4
4 5
4 6
Output:
7
Explanation of Sample:
As seen in the diagram, you seat the students in two groups of three with one empty desk in the middle.
【分析】有一群人,有的人互为朋友,现在有一排椅子,将这些人安排在椅子上,要求不是朋友的两个人不能坐在一起,即可以将他俩隔开或者中间放个空的椅子。
已知K个人最多有K-1对朋友。问最少需要多少椅子。
树的最小路径覆盖模板题。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
const int mod=1e9+;
int n,m;
int T,cnt;
int head[N],ans[N],vis[N];
bool mark[N];
struct edge{int to,next;}edg[N*];
void add(int u,int v)
{
edg[++cnt].to=v;edg[cnt].next=head[u];head[u]=cnt;
edg[++cnt].to=u;edg[cnt].next=head[v];head[v]=cnt;
}
void dfs(int x,int f)
{
ans[x]=vis[x]=;
int tot=;
for(int i=head[x];i!=-;i=edg[i].next)
{
if(edg[i].to==f)continue;
dfs(edg[i].to,x);
ans[x]+=ans[edg[i].to];
if(!mark[edg[i].to])tot++;
}
if(tot>=)ans[x]-=,mark[x]=;
else if(tot==)ans[x]--;
}
int main()
{
int u,v;
scanf("%d",&T);
while(T--)
{
cnt=;
met(head,-);
met(ans,);
met(mark,);
met(vis,);
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d",&u,&v);
add(u,v);
}
int anss=,ret=;;
for(int i=;i<=n;i++){
if(!vis[i]){
ret++;
dfs(i,);
vis[i]=;
anss+=ans[i]-;
}
}
printf("%d\n",anss+ret-+n);
}
return ;
}
SPOJ UOFTCG - Office Mates (树的最小路径覆盖)的更多相关文章
- SPOJ - UOFTCG 树的最小路径覆盖
//SPOJ - UOFTCG 树的最小路径覆盖 #include <bits/stdc++.h> #include <vector> using namespace std; ...
- Codeforces 618D Hamiltonian Spanning Tree(树的最小路径覆盖)
题意:给出一张完全图,所有的边的边权都是 y,现在给出图的一个生成树,将生成树上的边的边权改为 x,求一条距离最短的哈密顿路径. 先考虑x>=y的情况,那么应该尽量不走生成树上的边,如果生成树上 ...
- HDU 3861 The King’s Problem(tarjan连通图与二分图最小路径覆盖)
题意:给我们一个图,问我们最少能把这个图分成几部分,使得每部分内的任意两点都能至少保证单向连通. 思路:使用tarjan算法求强连通分量然后进行缩点,形成一个新图,易知新图中的每个点内部的内部点都能保 ...
- 【HDU1960】Taxi Cab Scheme(最小路径覆盖)
Taxi Cab Scheme Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- loj 1429(可相交的最小路径覆盖)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1429 思路:这道题还是比较麻烦的,对于求有向图的可相交的最小路径覆盖,首先要解决成环问 ...
- 【HDU3861 强连通分量缩点+二分图最小路径覆盖】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题目大意:一个有向图,让你按规则划分区域,要求划分的区域数最少. 规则如下:1.有边u到v以及有 ...
- POJ 3216 最小路径覆盖+floyd
Repairing Company Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 6646 Accepted: 178 ...
- POJ3020Antenna Placement(最小路径覆盖+重在构图)
Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7788 Accepted: 3880 ...
- POJ 3020 (二分图+最小路径覆盖)
题目链接:http://poj.org/problem?id=3020 题目大意:读入一张地图.其中地图中圈圈代表可以布置卫星的空地.*号代表要覆盖的建筑物.一个卫星的覆盖范围是其周围上下左右四个点. ...
随机推荐
- Android逆向之旅---静态方式分析破解视频编辑应用「Vue」水印问题
一.故事背景 现在很多人都喜欢玩文艺,特别是我身边的UI们,拍照一分钟修图半小时.就是为了能够在朋友圈显得逼格高,不过的确是挺好看的,修图的软件太多了就不多说了,而且一般都没有水印啥的.相比较短视频有 ...
- vector 进阶
http://classfoo.com/ccby/article/jnevK #include <iostream> #include <vector> #include &l ...
- taotao用户登录springMVC拦截器的实现
在springMVC中写拦截器,只需要两步: 一.写 java 拦截器类,实现 interceptor 拦截器接口. 二.在 springMVC 的xml配置文件中,配置我们创建的拦截器对象及其拦截目 ...
- LowercaseRoutesMVC ASP.NET MVC routes to lowercase URLs
About this Project Tired of your MVC application generating mixed-case URLs like http://mysite.com/H ...
- ng 构建
1.ng 构建和部署 构建:编译和合并ng build 部署:复制dist里面的文件到服务器 2.多环境的支持 配置环境package.json "scripts": { &quo ...
- hdu1527取石子游戏---(威佐夫博弈)
感谢 http://www.cnblogs.com/yuyixingkong/p/3362476.html 取石子游戏 Time Limit: 2000/1000 MS (Java/Others) ...
- Spring - IoC(2): 属性注入 & 构造注入
依赖注入是指程序运行过程中,如果需要另外的对象协作(访问它的属性或调用它的方法)时,无须在代码中创建被调用者,而是依赖于外部容器的注入. 属性注入(Setter Injection) 属性注入是指 I ...
- noip2013 提高组
T1 转圈游戏 题目传送门 果不其然 第一题还是模拟题 一波快速幂解决问题 #include<cstdio> #include<cstring> #include<alg ...
- bzoj 1089 SCOI2003严格n元树 递推
挺好想的,就是一直没调过,我也不知道哪儿的错,对拍也拍了,因为数据范围小,都快手动对拍了也不知道 哪儿错了.... 我们定义w[i]代表深度<=i的严格n元树的个数 那么最后w[d]-w[d-1 ...
- Codeforces 940F Machine Learning 带修改莫队
题目链接 题意 给定一个长度为\(n\)的数组\(a\),\(q\)个操作,操作分两种: 对于区间\([l,r]\),询问\(Mex\{c_0,c_1,c_2,⋯,c_{10^9}\}\),其中\(c ...