麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

SPOJ - UOFTCG(樹的路徑覆蓋,好題)

2019-11-11 04:57:46
字體:
來源:轉載
供稿:網友

題目鏈接

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 Kgraduate 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.

 Submit solution!
題意:

有N(N <= 100000)個學生,M對朋友關系,學生只能挨著他的朋友坐。

桌子排列成一條直線,可以讓一些桌子空出來.

數據保證對于任何含K(K<=N)個學生的集合,最多只有K-1對朋友。

求最少需要多少張桌子。

題解

這道題可以轉化為圖的最小路徑覆蓋。假設點數為n,最小路徑覆蓋條數為m,答案即為n+m-1。根據題意,發現數據是若干顆樹。

那么,對于一棵樹,怎么求最小路徑覆蓋呢?

有兩種方法,貪心和樹形dp,可參考博客:博客鏈接

樹形dp解至今還沒看懂==

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<queue>#include<stack>using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define pb push_back#define fi first#define se secondtypedef vector<int> VI;typedef long long ll;typedef pair<int,int> PII;const int inf=0x3fffffff;const ll mod=1000000007;const int maxn=100000+100;int head[maxn];struct edge{    int from,to,next;}e[maxn*2];   //int tol=0;void add(int u,int v){    e[++tol].to=v,e[tol].next=head[u],head[u]=tol;}int vis[maxn],sum[maxn],used[maxn];void dfs(int u,int fa){    vis[u]=1;    sum[u]=1;    int deg=0;    for(int i=head[u];i;i=e[i].next)    {        int v=e[i].to;        if(v==fa) continue;        dfs(v,u);        sum[u]+=sum[v];        if(!used[v]) deg++;    }    if(deg>=2) used[u]=1,sum[u]-=2;    else if(deg==1) sum[u]-=1;}int main(){    int cas;    scanf("%d",&cas);    while(cas--)    {        memset(vis,0,sizeof(vis));        memset(sum,0,sizeof(sum));        memset(head,0,sizeof(head));        memset(used,0,sizeof(used));        tol=0;        int n,m;        scanf("%d%d",&n,&m);        while(m--)        {            int u,v;            scanf("%d%d",&u,&v);            add(u,v),add(v,u);        }        int ans=0;        rep(i,1,n+1)        {            if(!vis[i]) dfs(i,0),ans+=sum[i];        }        ans=n+ans-1;        printf("%d/n",ans);    }    return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美黄色大片免费观看 | 久久亚洲成人 | 免费在线中文字幕 | 99影视电影电视剧在线播放 | 国产毛片在线高清视频 | 久久性生活免费视频 | 91羞羞| 一级做a爱片性色毛片高清 日本一区二区在线看 | 亚洲国产资源 | 91九色视频在线播放 | 99国产精品白浆在线观看免费 | 黄wwww| 免费毛片免费看 | 国产成人在线一区二区 | 国产一国产精品一级毛片 | 黄色大片高清 | 国产精品国产成人国产三级 | 姑娘第四集免费看视频 | 久久艹国产精品 | 成人区一区二区 | 美女在线视频一区二区 | 日本精品视频一区二区三区四区 | 欧美日韩高清在线观看 | free性欧美hd另类 | 俄罗斯hdxxx 日夜操天天干 | 免费毛片视频 | 精品一区二区三区中文字幕老牛 | 日本欧美一区二区三区视频麻豆 | 一级做a爱性色毛片免费1 | 欧美成人精品欧美一级 | 国产高潮失禁喷水爽到抽搐视频 | 欧美精品免费一区二区三区 | 亚洲第一页在线观看 | 在线播放免费播放av片 | 免费黄色入口 | 亚洲成人午夜精品 | 中文字幕涩涩久久乱小说 | 午夜视频在线免费 | 欧美一级毛片美99毛片 | 二级大黄大片高清在线视频 | 欧美视频一二三区 |