HDU 3849 by recognizing these Guys, We Find social Networks useful (dual connectivity)

Source: Internet
Author: User

HDU 3849 by recognizing these Guys, We Find social Networks useful

Topic links

Test instructions: It's plain to ask for a bridge without a map

Idea: string hash out, then double connect. Pay attention to a special sentence. The hypothesis is not a connected block. So the answer is 0.

Code:

#include <cstdio> #include <cstring> #include <string> #include <vector> #include <map> using namespace Std;const int N = 10005;const int M = 200005;int T, N, m;map<string, int> Hash;char a[20], b[20];str UCT Edge {int u, V, Id;bool iscut; Edge () {}edge (int u, int v, int id) {this->u = U;this->v = V;this->id = Id;this->iscut = false;}} Edge[m];int en, First[n], next[m], Hn;char name[n][20];void init () {en = HN = 0;memset (First,-1, sizeof (first)); Hash.clea R ();} int get (char *str) {if (!hash.count (str)) {strcpy (Name[hn], str); HASH[STR] = hn++;} return HASH[STR];} void Add_edge (int u, int v, int id) {Edge[en] = Edge (U, V, id); Next[en] = first[u];first[u] = en++;} int pre[n], dfn[n], dfs_clock, ans = 0;void dfs_cut (int u, int f) {pre[u] = Dfn[u] = ++dfs_clock;for (int i = first[u]; I + 1; i = Next[i]) {if (edge[i].id = = f) Continue;int v = edge[i].v;if (!pre[v]) {dfs_cut (V, edge[i].id);d fn[u] = min (Dfn[u], DF N[V]), if (Dfn[v] > Pre[u]) {Ans++;edge[i]. Iscut = Edge[i^1].iscut = true;}} else Dfn[u] = min (Dfn[u], pre[v]);}} void Find_cut () {memset (pre, 0, sizeof (PRE)), for (int i = 0; i < n; i++) if (!pre[i]) dfs_cut (i,-1);} int parent[n];int Find (int x) {return parent[x] = = x?

X:PARENT[X] = find (Parent[x]);} int main () {scanf ("%d", &t), while (t--) {init (); scanf ("%d%d", &n, &m); for (int i = 0; i < n; i++) Parent[i] = I;int cnt = n;for (int i = 0; i < m; i++) {scanf ("%s%s", A, B), int u = Get (a), V = Get (B), Add_edge (U, V, i); Add_edge (V, U, i); int pu = find (u), PV = Find (v), if (pu! = PV) {cnt--;p arent[pu] = PV;}} if (CNT > 1) {printf ("0\n"); continue;} ans = 0;find_cut ();p rintf ("%d\n", ans); for (int i = 0; i < en; i + = 2) if (edge[i].iscut) printf ("%s%s\n", Name[edge[i] . u], name[edge[i].v]);} return 0;}



HDU 3849 by recognizing these Guys, We Find social Networks useful (dual connectivity)

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.