Untrusted Patrol 14牡丹江網路賽C

來源:互聯網
上載者:User

標籤:2014網路賽   dfs   acm   zoj   

題意:給定一個n個點,m條邊的圖,其中k個點上有探測器

再給定一個探測器第一次被遍曆的序列,問是否存在一種遍曆順序使得滿足給定序列且遍曆完所有點


思路:從第一個被遍曆的探測器開始dfs,每次訪問到探測器遍停止,訪問到非探測器節點便搜下去。結束後判斷給定序列下個探測器是否被訪問過,若沒有,說明無法不通過 其他 探測器到達此探測器,無解。若被訪問過,繼續dfs此結點。

這樣dfs到的探測器保證了是從當前結點可以不通過任何探測器結點便可以訪問的,最後判斷一下是否每個節點都被訪問過,即圖是否連通


//#include <bits/stdc++.h>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <cmath>#include <vector>#include <iostream>#include <algorithm>using namespace std;//LOOP#define FF(i, a, b) for(int i = (a); i < (b); ++i)#define FE(i, a, b) for(int i = (a); i <= (b); ++i)#define FED(i, b, a) for(int i = (b); i>= (a); --i)#define REP(i, N) for(int i = 0; i < (N); ++i)#define CLR(A,value) memset(A,value,sizeof(A))#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)#define PB push_back//INPUT#define RI(n) scanf("%d", &n)#define RII(n, m) scanf("%d%d", &n, &m)#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)#define sqr(x) (x) * (x)typedef long long LL;typedef unsigned long long ULL;typedef vector <int> VI;const int INF = 0x3f3f3f3f;const LL lINF = 0x3f3f3f3f3f3f3f3fLL;const double eps = 1e-9;const int MOD = 1e9 + 7;const double PI = acos(-1.0);const int maxn = 100010;int n, m, k;bool is[maxn], vis[maxn];VI G[maxn], K;void init(){    CLR(is, 0);    CLR(vis, 0);    K.clear();    FE(i, 0, n + 1) G[i].clear();}void dfs(int u){    vis[u] = 1;    REP(i, G[u].size())    {        int v = G[u][i];        if (vis[v]) continue;        if (is[v])        {            vis[v] = 1;            continue;        }        else            dfs(v);    }}int main(){    int T, x, y;    RI(T);    while (T--)    {        RIII(n, m, k);        init();        REP(i, k)        {            RI(x);            is[x] = 1;        }        REP(i, m)        {            RII(x, y);            G[x].PB(y), G[y].PB(x);        }        int L, flag = 1;        RI(L);        REP(i, L)        {            RI(x);            K.PB(x);        }        if (L < k)        {            flag = 0;            goto end;        }        dfs(K[0]);        FF(i, 1, K.size())        {            if (vis[K[i]])                dfs(K[i]);            else            {                flag = 0;                goto end;            }        }        FE(i, 1, n)            if (!vis[i])            {                flag = 0;                break;            }        end:;        if (flag)            puts("Yes");        else            puts("No");    }    return 0;}/**/


Untrusted Patrol 14牡丹江網路賽C

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.