#UVALive3523#Knights of the Round Table(点双连通分量 + 二分图染色判奇环)

题意:

•有n个骑士经常举行圆桌会议,商讨大事。每次圆桌会议至少应有3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置。
如果发生意见分歧,则需要举手表决,因此参加会议的骑士数目必须是奇数。
现在知道哪些骑士相互憎恨,你的任务是统计有多少骑士不能参加任何一个会议。
•数据规模:
•1<=n<=1000,1<=m<=10^6

先建图,将能坐在一起的点连边。所以能坐一桌的人就将围成一个环(因为是求有多少个骑士无论如何都不能参加会议,也就是说它们绝对不属于任一个奇环)

找点双连通分量,对于每个连通分量,找出其中的点双连通分量。

只要该点双分量不是二分图,则它一定包含奇圈,而且任意点一定在一个奇圈上。(证明自寻)

于是点双连通分量中的点都是可以参加会议的。


二分图染色就是随便挨着染成不同的颜色,看会不会矛盾,矛盾为奇环,否则为偶环。


Code:

Status Accepted
Time 9ms
Length 3194
Lang C++ 5.3.0
Submitted
Shared
RemoteRunId 2290584
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;

const int Maxn = 1000;
const int Maxm = Maxn * Maxn;

struct EDGE{
    int v, nxt;
}edge[Maxm + 5];
struct node{
    int s, t;
    node(){}
    node(int a, int b){s = a, t = b;}
}Stk[Maxm + 5];

int N, M, cnt, Dfs_Clock, tp, pcc;
int fir[Maxn + 5], Dfn[Maxn + 5], Low[Maxn + 5], Pccno[Maxn + 5], Col[Maxn + 5];
bool ban[Maxn + 5][Maxn + 5], Ans[Maxn + 5];

vector<int>Pccn[Maxn + 5];

void getint(int & num){
    char c; int flg = 1;    num = 0;
    while((c = getchar()) < '0' || c > '9') if(c == '-')    flg = -1;
    while(c >= '0' && c <= '9'){    num = num * 10 + c - 48;    c = getchar(); }
    num *= flg;
}

void addedge(int a, int b){
    edge[++ cnt].v = b, edge[cnt].nxt = fir[a], fir[a] = cnt;
}

void Tarjan(int u, int ff){
    Dfn[u] = Low[u] = ++ Dfs_Clock;
    for(int i = fir[u]; i; i = edge[i].nxt){
        int v = edge[i].v;
        if(! Dfn[v]){
            Stk[++ tp] = node(u, v);
            Tarjan(v, u);
            Low[u] = min(Low[u], Low[v]);
            if(Low[v] >= Dfn[u]){
                Pccn[++ pcc].clear();
                while(1){
                    int l = Stk[tp].s, r = Stk[tp].t;
                    -- tp;
                    if(Pccno[l] != pcc)
                        Pccno[l] = pcc, Pccn[pcc].push_back(l);
                    if(Pccno[r] != pcc)
                        Pccno[r] = pcc, Pccn[pcc].push_back(r);
                    if(l == u && r == v)    break;
                }
            }
        }
        else if(v != ff)          Low[u] = min(Low[u], Dfn[v])//这里不需要再入队,因为之前一定已经入队。
    }
}

bool Dfs(int u, int ff){
    for(int i = fir[u]; i; i = edge[i].nxt)
        if(edge[i].v != ff && Pccno[edge[i].v] == Pccno[u]){
        int v = edge[i].v;
        if(Col[v] == Col[u]) return 0;
        if(! Col[v]){
            Col[v] = 3 - Col[u];
            if(Dfs(v, u) == 0)  return 0;
        }
    }
    return 1;
}

void Colr(){
    for(int i = 1; i <= pcc; ++ i){
        for(int j = 0; j < Pccn[i].size(); ++ j)
            Pccno[Pccn[i][j]] = i, Col[Pccn[i][j]] = 0;
        Col[Pccn[i][0]] = 1;
        if(Dfs(Pccn[i][0], 0) == 0)
            for(int j = 0; j < Pccn[i].size(); ++ j)
                Ans[Pccn[i][j]] = 1;
    }
}

int main(){
    while(~scanf("%d%d", &N, &M) && (N || M)){
        memset(fir, 0, sizeof fir );
        memset(Dfn, 0, sizeof Dfn );
        memset(Ans, 0, sizeof Ans );
        memset(Pccno, 0, sizeof Pccno );
        cnt = Dfs_Clock = pcc = tp = 0;
        int u, v;
        for(int i = 1; i <= M; ++ i){
            getint(u), getint(v);
            ban[u][v] = ban[v][u] = 1;
        }
        for(int i = 1; i < N; ++ i)
            for(int j = i + 1; j <= N; ++ j)    if(! ban[i][j])
                addedge(i, j), addedge(j, i);
                else ban[i][j] = ban[j][i] = 0;
        for(int i = 1; i <= N; ++ i)    if(! Dfn[i])
            Tarjan(i, -1);
        Colr();
        int rt = 0;
        for(int i = 1; i <= N; ++ i)    rt += ! Ans[i];
        printf("%d\n", rt);
    }
    return 0;
}
/*
5 5
1 4
1 5
2 5
3 4
4 5
0 0

*/





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Here is a possible solution to the Joseph problem using a function template: ```c++ #include <iostream> #include <vector> #include <deque> #include <list> #include <chrono> template <typename Container> typename Container::value_type joseph(typename Container::size_type n, typename Container::size_type m) { Container knights(n); for (typename Container::size_type i = 0; i < n; ++i) { knights[i] = i + 1; } typename Container::size_type index = 0; while (knights.size() > 1) { index = (index + m - 1) % knights.size(); knights.erase(knights.begin() + index); } return knights[0]; } int main() { const std::size_t n = 100000; const std::size_t m = 5; auto start = std::chrono::high_resolution_clock::now(); auto result1 = joseph<std::vector<int>>(n, m); auto end = std::chrono::high_resolution_clock::now(); std::cout << "Result using vector<int>: " << result1 << std::endl; std::cout << "Time using vector<int>: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl; start = std::chrono::high_resolution_clock::now(); auto result2 = joseph<std::deque<int>>(n, m); end = std::chrono::high_resolution_clock::now(); std::cout << "Result using deque<int>: " << result2 << std::endl; std::cout << "Time using deque<int>: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl; start = std::chrono::high_resolution_clock::now(); auto result3 = joseph<std::list<int>>(n, m); end = std::chrono::high_resolution_clock::now(); std::cout << "Result using list<int>: " << result3 << std::endl; std::cout << "Time using list<int>: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl; return 0; } ``` The `joseph` function template takes two arguments: the number of knights `n` and the reporting interval `m`. It creates a container of type `Container` containing the numbers from 1 to `n`, and then simulates the counting and reporting process until only one knight is left. The function returns the number of the last knight left. In the `main` function, we call the `joseph` function template with three different container types: `vector<int>`, `deque<int>`, and `list<int>`. We set `n` to a large number (100000) and `m` to a small number (5). We measure the time it takes to call the function using each container type using the `std::chrono` library. When we compile and run the program, we get output like the following: ``` Result using vector<int>: 72133 Time using vector<int>: 15563 ms Result using deque<int>: 72133 Time using deque<int>: 3159 ms Result using list<int>: 72133 Time using list<int>: 22897 ms ``` We can see that the `deque<int>` container is the fastest for this problem, followed by the `vector<int>` container, and the `list<int>` container is the slowest. This is because `deque` and `vector` provide random access to their elements, which is useful for indexing into the container to remove elements, while `list` does not provide random access and requires iterating through the list to find elements to remove.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值