scu 4439 Vertex Cover

Title:

Given n points and m edges, color several points so that each edge has at least one color, and ask at least how many points to color.

Ideas:

If it's a bipartite graph, it's the minimum point coverage, but this is a general graph.

The minimum coverage of the general graph is the npc problem, but this problem has a special condition, that is, each input edge is guaranteed to have at least one point less than or equal to 30, so it is enough to cover at most 30 points.

Then it can be solved by searching, and for a point, it is either covered or not covered.

If this point is covered, just search down directly;

If it is not covered, then either cover this point and search down directly; or do not cover this point, but cover all the points adjacent to this point, and then search down.

There are pruning, feasibility pruning and optimal pruning.

Code:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <vector>
 5 using namespace std;
 6 const int N = 505;
 7 int vis[N];
 8 vector<int> g[N];
 9 int tot;
10 int ans;
11 void dfs(int cu,int sum)
12 {
13     if (sum > ans) return;
14      if (with> all)
 15      {
 16          years = sum;
17          return ;
18      }
 19      if (vis [cu]) dfs (cu + 1 , sum);
20      else 
21      {
 22          vis [cu] ++ ;
23          dfs (with +1 , sum + 1 );
24          vis [cu] - ;
25          for (auto x: g [cu])
 26          {
 27              if (! Vis [x]) sum ++ ;
28             vis[x]++;
29         }
30         dfs(cu+1,sum);
31         for (auto x : g[cu])
32         {
33             vis[x]--;
34             if (!vis[x]) sum--;
35         }
36     }
37 }
38 int main()
39 {
40     int n,m;
41     while (scanf("%d%d",&n,&m) != EOF)
42     {
43         tot = 0;
44         ans = 10000;
45         memset(vis,0,sizeof(vis));
46         for (int i = 1;i <= n;i++) g[i].clear();
47         for (int i = 0;i < m;i++)
48         {
49             int x,y;
50             scanf("%d%d",&x,&y);
51             g[x].push_back(y);
52             g[y].push_back(x);
53         }
 54          tot = min( 30 ,n);
55          years = early;
56          dfs( 1 , 0 );
57          printf( " %d\n " ,years);
58      }
 59      return  0 ;
60 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325337094&siteId=291194637