[Codeforces 623A] Graph and String

來源:互聯網
上載者:User

標籤:return   tar   int   and   col   ret   http   main   一個   

[題目連結]

          http://codeforces.com/contest/623/problem/A

[演算法]

         首先 , 所有與其他節點都有連邊的節點需標號為‘b‘

         然後 , 我們任選一個節點 , 將其標號為‘a‘ , 然後標記所以該節點能到達的節點

         最後 , 我們需要檢查這張圖是否合法 , 只需枚舉兩個節點 , 若這兩個節點均為‘a‘或‘c‘ , 那麼 , 若兩個節點標號不同但有連邊 , 不合法 , 如果兩個節點標號相同但沒有連邊 , 也不合法

         時間複雜度 : O(N ^ 2)

[代碼]

        

#include<bits/stdc++.h>using namespace std;#define MAXN 510struct edge{        int to , nxt;} e[MAXN * MAXN * 2];int tot , n , m;int deg[MAXN],q[MAXN],head[MAXN];char ans[MAXN];bool finished[MAXN];bool g[MAXN][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;}inline void addedge(int u,int v){        tot++;        e[tot] = (edge){v,head[u]};        head[u] = tot;}int main(){                read(n); read(m);        for (int i = 1; i <= m; i++)        {                int u , v;                read(u); read(v);                deg[u]++; deg[v]++;                        addedge(u,v);                addedge(v,u);                g[u][v] = g[v][u] = true;        }        for (int i = 1; i <= n; i++)        {                if (deg[i] == n - 1)                {                        ans[i] = ‘b‘;                        finished[i] = true;                }        }        int l = 1 , r = 0;        for (int i = 1; i <= n; i++)        {                if (!finished[i])                {                        ans[i] = ‘a‘;                        finished[i] = true;                        q[++r] = i;                        break;                                }        }        while (l <= r)        {                int cur = q[l++];                for (int i = head[cur]; i; i = e[i].nxt)                {                        int v = e[i].to;                        if (!finished[v])                        {                                finished[v] = true;                                ans[v] = ‘a‘;                                q[++r] = v;                        }                }        }        for (int i = 1; i <= n; i++)        {                if (!finished[i])                        ans[i] = ‘c‘;        }        for (int i = 1; i <= n; i++)        {                if (ans[i] == ‘b‘) continue;                for (int j = 1; j <= n; j++)                {                        if (i == j || ans[j] == ‘b‘) continue;                        if (ans[i] == ans[j] && !g[i][j])                        {                                printf("No\n");                                return 0;                        }                        if (ans[i] != ans[j] && g[i][j])                        {                                printf("No\n");                                return 0;                        }                }        }        printf("Yes\n");        for (int i = 1; i <= n; i++) printf("%c",ans[i]);        printf("\n");                return 0;    }

 

[Codeforces 623A] Graph and String

聯繫我們

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