UOFTCG - Office Mates

no tags 

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 (树的最小路径覆盖)的更多相关文章

  1. SPOJ - UOFTCG 树的最小路径覆盖

    //SPOJ - UOFTCG 树的最小路径覆盖 #include <bits/stdc++.h> #include <vector> using namespace std; ...

  2. Codeforces 618D Hamiltonian Spanning Tree(树的最小路径覆盖)

    题意:给出一张完全图,所有的边的边权都是 y,现在给出图的一个生成树,将生成树上的边的边权改为 x,求一条距离最短的哈密顿路径. 先考虑x>=y的情况,那么应该尽量不走生成树上的边,如果生成树上 ...

  3. HDU 3861 The King’s Problem(tarjan连通图与二分图最小路径覆盖)

    题意:给我们一个图,问我们最少能把这个图分成几部分,使得每部分内的任意两点都能至少保证单向连通. 思路:使用tarjan算法求强连通分量然后进行缩点,形成一个新图,易知新图中的每个点内部的内部点都能保 ...

  4. 【HDU1960】Taxi Cab Scheme(最小路径覆盖)

    Taxi Cab Scheme Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. loj 1429(可相交的最小路径覆盖)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1429 思路:这道题还是比较麻烦的,对于求有向图的可相交的最小路径覆盖,首先要解决成环问 ...

  6. 【HDU3861 强连通分量缩点+二分图最小路径覆盖】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题目大意:一个有向图,让你按规则划分区域,要求划分的区域数最少. 规则如下:1.有边u到v以及有 ...

  7. POJ 3216 最小路径覆盖+floyd

    Repairing Company Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 6646   Accepted: 178 ...

  8. POJ3020Antenna Placement(最小路径覆盖+重在构图)

    Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7788   Accepted: 3880 ...

  9. POJ 3020 (二分图+最小路径覆盖)

    题目链接:http://poj.org/problem?id=3020 题目大意:读入一张地图.其中地图中圈圈代表可以布置卫星的空地.*号代表要覆盖的建筑物.一个卫星的覆盖范围是其周围上下左右四个点. ...

随机推荐

  1. [洛谷P2044][NOI2012]随机数生成器

    题目大意:给你$m,a,c,X_0,n,g$,求$X_{n+1}=(a\cdot X_n+c) \bmod{m}$,最后输出对$g$取模 题解:矩阵快速幂+龟速乘,这里用了$long\;double$ ...

  2. hdu4035 Maze 【期望dp + 数学】

    题目链接 BZOJ4035 题解 神题啊...orz 不过网上题解好难看,数学推导不写\(Latex\)怎么看..[Latex中毒晚期] 我们由题当然能很快写出\(dp\)方程 设\(f[i]\)表示 ...

  3. 使用google api material icons在网页中插入图标

    在<head></head>中加入这一句: <link rel='stylesheet' href='http://fonts.googleapis.com/icon?f ...

  4. Codeforces Round #520 (Div. 2) A. A Prank

    A. A Prank time limit per test   1 second memory limit per test    256 megabytes 题目链接:https://codefo ...

  5. Java多线程1:Java中sleep,wait,yield,join的区别

    1.sleep()方法 在指定时间内让当前正在执行的线程暂停执行,但不会释放“锁标志”.不推荐使用. sleep()使当前线程进入阻塞状态,在指定时间内不会执行. 2.wait()方法 在其他线程调用 ...

  6. MAVEN 编译打包测试 指定本地jar

    转载自:http://penuel.iteye.com/blog/1766102 maven对于互联网开发,进行版本管理有着不可或缺的作用;  而经常开发的程序猿直接联调或者依赖未上线或deploy的 ...

  7. php spl库的使用(PHP标准库)【摘抄引用】

    文章来源与推荐阅读:阮一峰--PHP SPL笔记  &&  PHP SPL使用方法和他的威力 1.SPL 是什么? SPL:standard php library php标准库,此 ...

  8. 精通javascript笔记(智能社)——数字时钟

    JS代码: <script type="text/javascript">    window.onload=function(){ //小于10的数字补零及数字转字符 ...

  9. eclipse配置文件内存设置

    1.-Xms64m -Xmx128m 2.配置文件的修改 http://wenku.baidu.com/link?url=spM-qCe0qHdhiykzwuzp-vBtcQrVtAzYiWe8uex ...

  10. DOM遍历查找结点

    一.遍历API(2个) 1.深度优先原则遍历NodeIterator 节点迭代器 创建遍历API对象: var iterator=document.createNodeIterator(开始的父节点对 ...