• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            poj 2672 Going from u to v or from v to u? 弱連通分量

            Going from u to v or from v to u?
            Time Limit: 2000MS Memory Limit: 65536K
            Total Submissions: 8032 Accepted: 1892

            Description

            In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors connecting some rooms. Each time, Wind choose two rooms x and y, and ask one of their little sons go from one to the other. The son can either go from x to y, or from y to x. Wind promised that her tasks are all possible, but she actually doesn't know how to decide if a task is possible. To make her life easier, Jiajia decided to choose a cave in which every pair of rooms is a possible task. Given a cave, can you tell Jiajia whether Wind can randomly choose two rooms without worrying about anything?

            Input

            The first line contains a single integer T, the number of test cases. And followed T cases. 

            The first line for each case contains two integers n, m(0 < n < 1001,m < 6000), the number of rooms and corridors in the cave. The next m lines each contains two integers u and v, indicating that there is a corridor connecting room u and room v directly. 

            Output

            The output should contain T lines. Write 'Yes' if the cave has the property stated above, or 'No' otherwise.

            Sample Input

            1
            3 3
            1 2
            2 3
            3 1
            

            Sample Output

            Yes
            題目:給定一個(gè)圖,問你是否對(duì)于任意的定點(diǎn)對(duì)x,y,能找到一條從x到y(tǒng) 或從y到x的路徑,(滿足一條即可);
            類型:弱連通分量。
            解法:
            這題花了很長時(shí)間,至少花了兩個(gè)小時(shí),到后來都想吐了,這讓我更惡心到了兩遍dfs求強(qiáng)連通分量的復(fù)雜。還好做出來了,雖然800+ms,還是挺爽的。
            n: 1000,  m: 6000
            原來什么都沒想就開始做,每次spfa(x,y),嘗試從x找到一條道y的路徑,果斷TLE。其實(shí)肯定TLE,不知道當(dāng)時(shí)怎么想的。
            這題先求強(qiáng)連通分量,縮點(diǎn),形成一棵新的樹,這樣主要是為了利用一些性質(zhì)。若圖是弱連通的,縮點(diǎn)后的樹必須滿足一下性質(zhì):
            1,形成的是一棵樹:這是必須的。否則從一棵樹的點(diǎn)到另一棵樹無法到達(dá)。
            2,這棵樹有且僅有一個(gè)點(diǎn)的入度是0, 如果有多個(gè)的話那么這些入度為0的點(diǎn)是不可以互相到達(dá)的。
            3, 從這個(gè)入度為0的點(diǎn)進(jìn)行dfs,找到的是一條鏈,就是不會(huì)有分叉,如果有的話,則兩個(gè)分叉的點(diǎn)互相不可達(dá)。
            (這點(diǎn)其實(shí)我不明白,應(yīng)該是找條最長鏈的)
            代碼超級(jí)亂:
            
            

            Source Code

            Problem: 2762 User: hehexiaobai
            Memory: 9084K Time: 891MS
            Language: C++ Result: Accepted
            • Source Code
              #include<iostream>
                  #include<cstring>
                  #include<cstdio>
                  using namespace std;
                  const int MAXV = 1005;
                  const int MAXE = 6005;
                  int adj[MAXV][MAXV];
                  int adj_op[MAXV][MAXV];
                  int f[MAXV];
                  int sblg[MAXV];
                  int m, n, cntf,nstrong;
                  bool visit[MAXV];
                  bool strong[MAXV][MAXV];
                  int in[MAXV];
                  void dfs1(int u)
                  {
                  visit[u] = true;
                  for(int i = 1; i <= adj[u][0]; i ++)
                  if(!visit[adj[u][i]])dfs1(adj[u][i]);
                  f[cntf] = u;
                  cntf --;
                  }
                  void dfs2(int u, int nstrong)
                  {
                  visit[u] = true;
                  sblg[u] = nstrong;
                  for(int i = 1; i <= adj_op[u][0]; i ++)
                  if(!visit[adj_op[u][i]])dfs2(adj_op[u][i],nstrong);
                  }
                  bool flag ;
                  void dfs(int u)
                  {
                  visit[u] = true;
                  int c = 0;
                  for(int i = 1; i <= nstrong; i ++)
                  {
                  if(strong[u][i])
                  {
                  dfs(i);
                  c ++;
                  }
                  }
                  if(c > 1)flag = false;
                  }
                  int main()
                  {
                  int t,i,j, u, v;
                  scanf("%d",&t);
                  while(t--)
                  {
                  memset(adj,0,sizeof adj);
                  memset(f,0,sizeof f);
                  memset(adj_op, 0, sizeof (adj_op));
                  scanf("%d %d",&n, &m);
                  for(i = 0; i < m; i ++)
                  {
                  scanf("%d %d",&u, &v);
                  adj[u][0] ++;
                  adj[u][adj[u][0]] = v;
                  adj_op[v][0] ++;
                  adj_op[v][adj_op[v][0]] = u;
                  }
                  memset(visit, 0, sizeof visit);
                  cntf = n ;
                  for(i = 1; i <= n; i ++)
                  if(!visit[i]) dfs1(i);
                  memset(visit, 0, sizeof visit);
                  memset(sblg, 0, sizeof (sblg));
                  nstrong = 0;
                  for(i = 1; i <= n;i ++)
                  if(!visit[f[i]])
                  {
                  nstrong ++ ;
                  dfs2(f[i], nstrong);
                  }
                  if(nstrong == 1)
                  {
                  printf("Yes\n");
                  continue;
                  }
                  memset(in, 0, sizeof in);
                  memset(strong, 0,sizeof strong);
                  for( i  = 1; i <= n; i ++)
                  for(j = 1; j <= adj[i][0]; j++ )
                  if(sblg[i] != sblg[adj[i][j]])
                  {
                  in[sblg[adj[i][j]]] = true;
                  strong[sblg[i]][sblg[adj[i][j]]] = true;
                  }
                  int index = -1;
                  for(i = 1;i <= n; i ++)
                  if(in[sblg[i]] == false)
                  {
                  index = sblg[i];
                  break;
                  }
                  flag = true;
                  memset(visit, 0, sizeof visit);
                  dfs(index);
                  /*	for(i = 1; i <= nstrong; i ++,cout << endl)
                  for(j = 1; j <= nstrong; j ++)
                  cout << strong[i][j]<<' ';
                  */
                  for(i = 1; i <= nstrong;i ++)
                  if(visit[i] == false)
                  flag = false;
                  if(flag == true)
                  {
                  printf("Yes\n");
                  }
                  else printf("No\n");
                  }
                  return 0;
                  }

            posted on 2010-12-11 22:48 田兵 閱讀(655) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 圖論題

            <2025年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(2)

            隨筆分類(65)

            隨筆檔案(65)

            文章檔案(2)

            ACM

            搜索

            積分與排名

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            国产精品久久久久久久久免费| 嫩草伊人久久精品少妇AV| 久久精品中文字幕无码绿巨人| 亚洲中文精品久久久久久不卡| 国内高清久久久久久| 亚洲一区二区三区日本久久九| 欧美日韩成人精品久久久免费看| 久久天天躁狠狠躁夜夜96流白浆| 久久午夜无码鲁丝片秋霞| 91精品国产综合久久四虎久久无码一级| 日本久久中文字幕| 久久精品三级视频| 久久精品二区| 久久精品国产亚洲Aⅴ蜜臀色欲| 婷婷综合久久中文字幕蜜桃三电影| 国产精品久久国产精品99盘| 亚洲国产另类久久久精品 | 精品久久久久久久久久久久久久久 | 激情伊人五月天久久综合| 色8激情欧美成人久久综合电| 韩国三级中文字幕hd久久精品| 国产精品久久自在自线观看| 午夜人妻久久久久久久久| 久久狠狠色狠狠色综合| 国产精品免费久久久久久久久| 亚洲综合久久综合激情久久| 久久精品国产亚洲Aⅴ蜜臀色欲| 三级片免费观看久久| 精品久久无码中文字幕| 97精品国产97久久久久久免费| 2020最新久久久视精品爱| 2022年国产精品久久久久| 国产成人精品久久亚洲高清不卡| 久久精品亚洲乱码伦伦中文| 亚洲人成无码www久久久| 91精品国产综合久久婷婷| 一本色综合久久| 久久久99精品成人片中文字幕 | 噜噜噜色噜噜噜久久| 久久99精品国产99久久| 人妻无码αv中文字幕久久琪琪布 人妻无码精品久久亚瑟影视 |