首页 > 其他分享 >HDU 5639 Deletion

HDU 5639 Deletion

时间:2022-11-09 18:34:05浏览次数:46  
标签:HDU return 5639 int tot edge Deletion first dis


Problem Description


G with  n vertices and  m edges. Every time, you can select several edges and delete them. The edges selected must meet the following condition: let  G′ be graph induced from these edges, then every connected component of  G′


 



Input


T indicating the number of test cases. For each test case:

The first line contains two integers  n and  m  (1≤n≤2000,0≤m≤2000) -- the number of vertices and the number of edges.

For the next  m lines, each line contains two integers  ui and  vi, which means there is an undirected edge between  ui and  vi  (1≤ui,vi≤n,ui≠vi).

The sum of values of  n in all test cases doesn't exceed  2⋅104. The sum of values of  m in all test cases doesn't exceed  2⋅104.


 



Output


For each test case, output the minimum number of deletion needed.


 



Sample Input


3 4 2 1 2 1 3 4 5 1 2 1 3 1 4 2 3 2 4 4 4 1 2 2 3 3 4 4 1


 



Sample Output


1 2 1



表示想不到,参考的是题解中的解法2,把原图中的边新建成一个点与原图中的两个点相连建成二分图,然后二分答案验证时否完全匹配


#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<functional>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=2e3+10;
const int mod=1e9+7;
int T,n,m,x[maxn],y[maxn];

struct MaxFlow
{
const static int maxe = 2e6 + 10; //边数
const static int maxp = 1e5 + 10; //点数
const static int INF = 0x7FFFFFFF;
struct Edges
{
int x, f;
Edges(){}
Edges(int x, int f) :x(x), f(f){}
}edge[maxe];
int first[maxp], next[maxe], dis[maxp], tot, work[maxp], n;

void clear(int x){ n = x; tot = 0; for (int i = 0; i <= n; i++) first[i] = -1; }

void AddEdge(int s, int t, int f)
{
edge[tot] = Edges(t, 0); next[tot] = first[s]; first[s] = tot++;
edge[tot] = Edges(s, f); next[tot] = first[t]; first[t] = tot++;
}

bool bfs(int s, int t)
{
for (int i = 0; i <= n; i++) dis[i] = -1;
queue<int> p; p.push(s); dis[s] = 0;
while (!p.empty())
{
int q = p.front(); p.pop();
for (int i = first[q]; i != -1; i = next[i])
{
if (edge[i ^ 1].f&&dis[edge[i].x] == -1)
{
p.push(edge[i].x);
dis[edge[i].x] = dis[q] + 1;
if (dis[t] != -1) return true;
}
}
}
return false;
}

int dfs(int s, int t, int low)
{
if (s == t) return low;
for (int &i = work[s], x; i >= 0; i = next[i])
{
if (dis[s] + 1 == dis[edge[i].x] && edge[i ^ 1].f && (x = dfs(edge[i].x, t, min(low, edge[i ^ 1].f))))
{
edge[i].f += x; edge[i ^ 1].f -= x; return x;
}
}
return 0;
}

int dinic(int s, int t)
{
int maxflow = 0, inc = 0;
while (bfs(s, t))
{
for (int i = 0; i <= n; i++) work[i] = first[i];
while (inc = dfs(s, t, INF)) maxflow += inc;
}
return maxflow;
}
}solve;

bool check(int flow)
{
solve.clear(n+m+1);
for (int i=1;i<=m;i++)
{
solve.AddEdge(i,0,1);
solve.AddEdge(m+x[i],i,1);
solve.AddEdge(m+y[i],i,1);
}
for (int i=m+1;i<=m+n;i++) solve.AddEdge(n+m+1,i,flow);
return solve.dinic(n+m+1,0)==m;
}

int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++) scanf("%d%d",&x[i],&y[i]);
if (!m) {printf("0\n"); continue;}
int q=1,h=m;
while (q<=h)
{
int mid=q+h>>1;
if (check(mid)) h=mid-1; else q=mid+1;
}
printf("%d\n",q);
}
return 0;
}



标签:HDU,return,5639,int,tot,edge,Deletion,first,dis
From: https://blog.51cto.com/u_15870896/5837727

相关文章

  • HDU 5637 Transform
    ProblemDescriptionn integersaregiven.Foraninteger x youcandothefollowingoperations:+letthebinaryrepresentationof x be ......
  • HDU 1403 Longest Common Substring
    ProblemDescriptionGiventwostrings,youhavetotellthelengthoftheLongestCommonSubstringofthem.Forexample:str1=bananastr2=ciana......
  • HDU 4658 Integer Partition
    ProblemDescriptionGivenn,k,calculatethenumberofdifferent(unordered)partitionsofnsuchthatnopartisrepeatedkormoretimes.  ......
  • HDU 5638 Toposort
    ProblemDescriptionn verticesand m edges.Youareallowedtodeleteexact k InputT indicatingthenumberoftestcases.Fore......
  • HDU 4651 Partition
    ProblemDescriptionHowmanywayscanthenumbers1to15beaddedtogethertomake15?Thetechnicaltermforwhatyouareaskingisthe"numberofpart......
  • HDU 1028
    如果只有\(1\)数字,多项式为:\(1+x+x^2+x^3+\ldots\)。如果只有\(2\)数字,多项式为:\(1+x^2+x^4+x^6+\ldots\)。……如果只有\(k\)数字,多项式为:\(1+x^k+x^{2k}+x^{3......
  • B - K-th Number HDU - 6231 (二分 尺取)
    WindowsSource2017中国大学生程序设计竞赛-哈尔滨站题意给一个数组,在所有长度大于等于k的区间内,找出第\(k\)大的数,放到另一个数组中,然后在新数组中找到第M大的数。思......
  • HDU-1260 Tickets
    感觉题目还是比较水的,我这个蒟蒻也能写出来hh。思路:f[i]是前i个人(包含第i个)买票需要花费的总时间,第i个人买票所需时间,可以自己单买(f[i-1]+a[i]),也可以和前面那个人拼......
  • HDU 1686Oulipo ———————Hash or KMP
    OulipoOulipoTimeLimit:3000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):22302    AcceptedSubmission(s):86......
  • HDU 2050折线分割平面(递推)
    折线分割平面TimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):36479    AcceptedSubmission(s):244......