SPOJ UOFTCG - Office Mates (树的最小路径覆盖)

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:
16 51 21 31 44 54 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 = 200050;
const int M = 24005;
const int mod=1e9+7;
int n,m;
int T,cnt;
int head[N],ans[N],vis[N];
bool mark[N];
struct edge{int to,next;}edg[N*2];
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]=1;
    int tot=0;
    for(int i=head[x];i!=-1;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>=2)ans[x]-=2,mark[x]=1;
    else if(tot==1)ans[x]--;
}
int main()
{
    int u,v;
    scanf("%d",&T);
    while(T--)
    {
        cnt=0;
        met(head,-1);
        met(ans,0);
        met(mark,0);
        met(vis,0);
        scanf("%d%d",&n,&m);
        while(m--)
        {
            scanf("%d%d",&u,&v);
            add(u,v);
        }
        int anss=0,ret=0;;
        for(int i=1;i<=n;i++){
            if(!vis[i]){
                ret++;
                dfs(i,0);
                vis[i]=1;
                anss+=ans[i]-1;
            }
        }
        printf("%d\n",anss+ret-1+n);
    }
    return 0;
}
时间: 2024-08-13 18:53:17

SPOJ UOFTCG - Office Mates (树的最小路径覆盖)的相关文章

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

题意:给出一张完全图,所有的边的边权都是 y,现在给出图的一个生成树,将生成树上的边的边权改为 x,求一条距离最短的哈密顿路径. 先考虑x>=y的情况,那么应该尽量不走生成树上的边,如果生成树上有一个点的度数是n-1,那么必然需要走一条生成树上的边,此时答案为x+y*(n-2). 否则可以不走生成树上的边,则答案为y*(n-1). 再考虑x<y的情况,那么应该尽量走生成树上的边,由于树上没有环,于是我们每一次需要走树的一条路,然后需要从非生成树上的边跳到树的另一个点上去, 显然跳的越少越好,于

求一棵树的最小路径覆盖

相关题目:http://codeforces.com/problemset/problem/618/D 有向图的最小路径覆盖(所有点)可以用二分图来解,n-最大匹配. 无向图的最小路径覆盖(所有点)似乎是比较困难的问题 那么对于特殊的无向图 - '树'来说,求它的最小路径覆盖有什么好用的方法呢? 首先我们可以考虑用dp求解,维护每棵子树留下向上可延伸路径的情况下能获得的最小路径覆盖是多少f[i][1],以及每棵子树不留下向上可延伸路径的情况下能获得的最小路径覆盖f[i][0].转移在这里不多赘述

hiho 第118周 网络流四&#183;最小路径覆盖

描述 国庆期间正是旅游和游玩的高峰期. 小Hi和小Ho的学习小组为了研究课题,决定趁此机会派出若干个调查团去沿途查看一下H市内各个景点的游客情况. H市一共有N个旅游景点(编号1..N),由M条单向游览路线连接.在一个景点游览完后,可以顺着游览线路前往下一个景点. 为了避免游客重复游览同一个景点,游览线路保证是没有环路的. 每一个调查团可以从任意一个景点出发,沿着计划好的游览线路依次调查,到达终点后再返回.每个景点只会有一个调查团经过,不会重复调查. 举个例子: 上图中一共派出了3个调查团: 1

hdu3861 强连通+最小路径覆盖

题意:有 n 个点,m 条边的有向图,需要将这些点分成多个块,要求:如果两点之间有路径能够互相到达,那么这两个点必须分在同一块:在同一块内的任意两点相互之间至少要有一条路径到达,即 u 到达 v 或 v 到达 u:每个点都只能存在于单独一个块内.问最少需要划分多少块. 首先,对于如果两点之间能够相互到达则必须在同一块,其实也就是在同一个强连通分量中的点必须在同一块中,所以首先就是强连通缩点.然后在同一块内的任意两点之间要有一条路,那么其实就是对于一块内的强连通分量,至少要有一条路径贯穿所有分量.

COGS728. [网络流24题] 最小路径覆盖问题

算法实现题8-3 最小路径覆盖问题(习题8-13) ´问题描述: 给定有向图G=(V,E).设P是G的一个简单路(顶点不相交)的集合.如果V中每个顶点恰好在P的一条路上,则称P是G的一个路径覆盖.P中路径可以从V的任何一个顶点开始,长度也是任意的,特别地,可以为0.G的最小路径覆盖是G的所含路径条数最少的路径覆盖.设计一个有效算法求一个有向无环图G的最小路径覆盖. 提示: 设V={1,2,...  ,n},构造网络G1=(V1,E1)如下: 每条边的容量均为1.求网络G1的(x0,y0)最大流.

【最小路径覆盖】BZOJ2150-部落战争

[题目大意] 给出一张图,'*'表示不能走的障碍.已知每只军队可以按照r*c的方向行军,且军队与军队之间路径不能交叉.问占据全部'.'最少要多少支军队? [思路] 首先注意题意中有说“军队只能往下走”,弄清楚方向. 从某点往它能走的四个点走一趟,连边.最小路径覆盖=总数-二分图最大匹配. 哦耶!老了,连匈牙利的板子都敲错orzzzzzz 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int MAXN=55; 4 int m,n

有向无环图(DAG)的最小路径覆盖

DAG的最小路径覆盖 定义:在一个有向图中,找出最少的路径,使得这些路径经过了所有的点. 最小路径覆盖分为最小不相交路径覆盖和最小可相交路径覆盖. 最小不相交路径覆盖:每一条路径经过的顶点各不相同.如图,其最小路径覆盖数为3.即1->3>4,2,5. 最小可相交路径覆盖:每一条路径经过的顶点可以相同.如果其最小路径覆盖数为2.即1->3->4,2->3>5. 特别的,每个点自己也可以称为是路径覆盖,只不过路径的长度是0. DAG的最小不相交路径覆盖 算法:把原图的每个点

hdu 3861 The King’s Problem (强连通+最小路径覆盖)

The King's Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1637    Accepted Submission(s): 600 Problem Description In the Kingdom of Silence, the king has a new problem. There are N cit

POJ 3020 Antenna Placement ,二分图的最小路径覆盖

题目大意: 一个矩形中,有N个城市'*',现在这n个城市都要覆盖无线,若放置一个基站,那么它至多可以覆盖相邻的两个城市. 问至少放置多少个基站才能使得所有的城市都覆盖无线? 无向二分图的最小路径覆盖 = 顶点数 –  最大二分匹配数/2 路径覆盖就是在图中找一些路径,使之覆盖了图中的所有顶点,且任何一个顶点有且只有一条路径与之关联: #include<cstdio> #include<cstring> #include<vector> #include<algor