Exercise 6-14 The problem of inspectors UVa12118

Source: Internet
Author: User

1. Title Description: Click to open the link

2. Problem-solving ideas: Using DFS and Euler roads to solve problems. In fact, the need to find some path, so that the path and the required paths to form the Euler road (note is not Euler circuit, "Euler Road" refers to the path in addition to the beginning and end of the other points on the route is equal to the degree of the road). The first is to use DFS to determine connectivity, there is another reason to use DFS here is to count the number of points of odd degrees , because such a point will need to add a road so that its degree becomes even, in line with the requirements of Euler road. Since adding a path causes the two endpoints to become 2 degrees at the same time, and the number of start and end points is odd, the count is subtracted by 2, so the final path total is e+ (res-2)/2. where (res-2)/2 is the last number of paths added.

3. Code:

#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std; #define N 1000+10int V, E, t;vector< Int>road[n];int vis[n];void init () {memset (Vis, 0, sizeof (VIS)), for (int i = 0; i < N; i++) road[i].clear ();} int dfs (int n) {if (Vis[n]) return 0;int cnt = 0;vis[n] = 1;cnt + = (road[n].size () & 1);//statistic degree is an odd point for (int i = 0; I &lt ; Road[n].size (); i++) cnt + = DFS (road[n][i]); return CNT;} int solve () {int res = 0;for (int i = 1; I <= v;i++) if (!vis[i] &&! Road[i].empty ())//Note Be sure to prevent repeated accumulation! Res + = max (Dfs (i), 2);//statistics of the number of points with odd degrees return t* (Max ((res-2)/2, 0) + e);//The final answer is E plus the number of newly added paths (res-2)/2}int main () {//freop En ("T.txt", "R", stdin); int rnd =0;while (Init (), CIN >> V >> E >> T, V | | E | | T) {int x, y;for (int i = 0; i < E; i++) {scanf ("%d%d", &x, &y); Road[x].push_back (y); Road[y].push_back (x);} printf ("Case%d:%d\n", ++rnd, Solve ());} return 0;}

Exercise 6-14 The problem of inspectors UVa12118

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.