UVa 12118 Nspector ' s dilemma (tectonic +dfs+ Euler circuit)

Source: Internet
Author: User

Test instructions: Given n points, the E-bar and the length of each edge t, each of the two points are connected, allowing you to find a shortest path through the e-side.

Analysis: Just began to think of the connection, and then the corresponding several pieces add up, but, the second example is but, then a think, then there is a Euro-loop has to add 1 ah.

Also want to judge each time is not the euro-pull circuit, how to judge is the problem, because do not know which is connected in a piece, you have to find again, trouble ah ....

Later on the internet to look at the puzzle, the original is to construct Ah, that is, each connected block is structured into a European pull circuit, then minus the end point, it can be fully connected.

It's a good way, the Euler loop satisfies the degree of each point is even, that is, if it is not an even number, then we have to add a man, and finally calculate how many we added,

Then add the original title of the article E, is the last number.

The code is as follows:

#include <iostream> #include <cstdio> #include <vector> #include <cstring>using namespace std;    const int MAXN = 10;int T, N, e;vector<int> g[maxn];bool vis[maxn];int dfs (int u) {if (Vis[u]) return 0;    Vis[u] = true;    int ans = (g[u].size () & 1);    for (int i = 0; i < g[u].size (); ++i) ans + = DFS (g[u][i]); return ans;}    int solve () {int ans = 0; for (int i = 1; I <= n; ++i) if (!    G[i].empty () &&!vis[i]) ans + = max (Dfs (i), 2); Return Max ((ans-2)/2, 0) + e;}    int main () {//Freopen ("In.txt", "R", stdin);    int kase = 0;        while (scanf ("%d%d", &n, &e, &t) = = 3) {if (!n &&!e &&!t) break;        int u, v;        for (int i = 1; I <= n; ++i) g[i].clear ();        memset (Vis, 0, sizeof (VIS));            for (int i = 0; i < e; ++i) {scanf ("%d%d", &u, &v);            G[u].push_back (v);        G[v].push_back (U); } printf ("Case %d:%d\n ", ++kase, T * solve ()); } return 0;}

UVa 12118 Nspector ' s dilemma (tectonic +dfs+ Euler circuit)

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.