CF 161 div2 D

來源:互聯網
上載者:User

就是已知每個點的度數至少為k,要求在圖中找一個環,長度為k。這個題可以用最長路的方法去思考,假設圖中一條最長路,包含v1,v2,...vn,那麼對於vn,它的所有相鄰的點一定都被包含在這條最長路中,否則這個點接在vn後面,就是一條更長的路。那麼對於vn,找到它所有相鄰的點中在這條最長路裡離它最遠的點vl,因為vn度數為k,所以vl到vn至少有n+1個點,且首尾相連夠成一個環。這樣就可以從任意一個點,例如1開始搜一個長度大於k的環即可,因為即使一個圖是有多個塊的,每個塊中都至少存在一個長度為k的環。

#include <iostream>#include <cstdio>#include <cstring>#include <cstdio>#include <cstdlib>#include <map>#include <set>#include <vector>#include <queue>#include <stack>#include <algorithm>#include <list>using namespace std;typedef long long LL;const int maxn = 100000 + 5;vector<int> G[maxn];int fa[maxn];int vis[maxn];int chuo[maxn];int k,tag;void dfs(int x,int cnt){    for(int i = 0;i < G[x].size();i++){        if(tag == 1) return;        int u = G[x][i];        if(!vis[u]){            vis[u] = 1;            fa[u] = x;            chuo[u] = cnt+1;            dfs(u,cnt+1);            vis[u] = 0;        }        else if(chuo[x]-chuo[u] >= k){            cout << chuo[x]-chuo[u] + 1 << endl;            int tem = x;            cout << x << ' ';            while(fa[tem] != u){                cout << fa[tem] << ' ';                tem = fa[tem];            }            cout << u << endl;            tag = 1;            return;        }    }}int main(){    int n,m;    while(cin >> n >> m >> k){        for(int i = 1;i <= n;i++) G[i].clear();        while(m--){            int x,y;            cin >> x >> y;            G[x].push_back(y);            G[y].push_back(x);        }        memset(vis,0,sizeof(vis));        vis[1] = 1;        chuo[1] = 0;        tag = 0;        dfs(1,0);    }    return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.