[Codeforces 1037E] Trip

來源:互聯網
上載者:User

標籤:問題   type   ret   href   dig   pac   print   連結   color   

[題目連結]

         http://codeforces.com/problemset/problem/1037/E

[演算法]

        首先離線 , 將問題倒過來考慮 , 轉化為 : 每次刪除一條邊 , 此時最多有多少人蔘加旅行

        假設所有點都選取 , 那麼 ,如果一個點的度數 < k , 顯然這個點不能選 , 我們需要刪除它和所有與它相鄰的邊

        顯然 , 所有的點和邊最多隻會被刪去一次 , 故時間複雜度為O(N + M)

[代碼]

        

#include<bits/stdc++.h>using namespace std;const int MAXN = 2e5 + 10;int n , m , k;int deg[MAXN],res[MAXN],x[MAXN],y[MAXN],q[MAXN];bool visited[MAXN],del[MAXN];vector< int > a[MAXN],b[MAXN];template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }template <typename T> inline void read(T &x){    T f = 1; x = 0;    char c = getchar();    for (; !isdigit(c); c = getchar()) if (c == ‘-‘) f = -f;    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - ‘0‘;    x *= f;}int main(){                read(n); read(m); read(k);        for (int i = 1; i <= m; i++)        {                read(x[i]); read(y[i]);                a[x[i]].push_back(y[i]);                a[y[i]].push_back(x[i]);                b[x[i]].push_back(i);                b[y[i]].push_back(i);                deg[x[i]]++; deg[y[i]]++;                }        int l = 1 , r = 0 ,ans = n;        for (int i = 1; i <= n; i++)        {                if (deg[i] < k)                {                        visited[i] = true;                        q[++r] = i;                        ans--;                }        }        for (int i = m; i >= 1; i--)        {                while (l <= r)                {                        int u = q[l++];                        for (unsigned j = 0; j < a[u].size(); j++)                        {                                if (!del[b[u][j]])                                {                                        deg[u]--;                                        deg[a[u][j]]--;                                        del[b[u][j]] = true;                                        if (!visited[a[u][j]] && deg[a[u][j]] < k)                                        {                                                ans--;                                                visited[a[u][j]] = true;                                                q[++r] = a[u][j];                                                                                        }                                }                        }                }                res[i] = ans;                if (!del[i])                {                        del[i] = true;                        deg[x[i]]--;                        deg[y[i]]--;                        if (!visited[x[i]] && deg[x[i]] < k)                        {                                visited[x[i]] = true;                                ans--;                                q[++r] = x[i];                        }                        if (!visited[y[i]] && deg[y[i]] < k)                        {                                visited[y[i]] = true;                                ans--;                                q[++r] = y[i];                        }                }        }        for (int i = 1; i <= m; i++) printf("%d\n",res[i]);                return 0;    }

 

[Codeforces 1037E] Trip

聯繫我們

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