Codeforce Cthulhu

题目:Cthulhu

题意:就是让你找一个简单的环,而且这个环不需要经过所有的点,只需要经过某些点就可以了,如果存在输出 FHTAGN! ,否则输出NO
思路:简单的环就是只存在一个环,不存在多种环的情况,所以这种情况下我们我可以判断n个点是否连通,然后在判断边数是否为n,为什么?假设只经过6个点5条边已经连通了这种情况就是一条相连的直线的那种情况,那么再多一个点,那么这个图就存在环,不要想的太复杂了。判断这些点是否相连,我们可以采用并查集或者dfs,用并查集或者dfs判断是否经过所有的点,如果经过所有的点并且边的个数为n那么就输出 FHTAGN! ,否则就输出"red">NO。
代码:dfs

#include<bits/stdc++.h>
using namespace std;

const int maxn = 205;//记住数组一定大小一定要开二倍,因为是双向建边
int h[maxn], e[maxn], ne[maxn], id = 0;
void add(int u, int v)//数组模拟链式前向星
{
    e[id] = v;
    ne[id] = h[u];
    h[u] = id++;
}
int n, m, u, v;
int vis[maxn];
void dfs(int u)//深搜
{
    vis[u] = 1;
    for(int i = h[u]; i != -1; i = ne[i])
    {
        int t = e[i];
        if(!vis[t])
        {
            dfs(t);
        }
    }
}
int main()
{
    cin>>n>>m;
    memset(h, -1, sizeof(h));
    for(int i = 0; i < m; i++)
    {
        cin>>u>>v;
        add(u, v);
        add(v, u);
    }
    memset(vis, 0, sizeof(vis));
    dfs(1);
    int flag = 0;
    for(int i = 1; i <= n; i++)//判断是否有的边没有遍历
    {
        if(!vis[i])flag = 1;
    }
    if(flag == 0 && m == n)cout<<"FHTAGN!"<<endl;
    else cout<<"NO"<<endl;
    return 0;
}

代码:并查集

#include<bits/stdc++.h>
using namespace std;
const int maxn = 105;
int f[maxn];
int Find(int x){//并查集
    int p = x, tmp;
    while(x != f[x])
        x = f[x];
    while(p != x){
        tmp = f[x];
        f[x] = x;
        p = tmp;
    }
    return x;
}
int n, m;
void init(){
     for(int i = 1; i <= n; i++)f[i] = i;
}
int main(){
    cin>>n>>m;
    init();
    int u, v;
    for(int i = 1; i <= m; i++){
        cin>>u>>v;
        int fx, fy;
        fx = Find(u);
        fy = Find(v);
        if(fx != fy)f[fx] = fy;
    }
    int tmp = Find(1);
    int sum = 0;
    for(int i = 1; i <= n; i++){
        if(Find(i) == i)sum++;//判断有几个公共祖先,
    }
    if(sum == 1 && m == n)cout<<"FHTAGN!"<<endl;//如果只有一个祖先,那么整个图就是联通的
    else cout<<"NO"<<endl;
return 0;
}

### Codeforces Problem or Contest 998 Information For the specific details on Codeforces problem or contest numbered 998, direct references were not provided within the available citations. However, based on similar structures observed in other contests such as those described where configurations often include constraints like `n` representing numbers of elements with defined ranges[^1], it can be inferred that contest or problem 998 would follow a comparable format. Typically, each Codeforces contest includes several problems labeled from A to F or beyond depending on the round size. Each problem comes with its own set of rules, input/output formats, and constraint descriptions. For instance, some problems specify conditions involving integer inputs for calculations or logical deductions, while others might involve more complex algorithms or data processing tasks[^3]. To find detailed information regarding contest or problem 998 specifically: - Visit the official Codeforces website. - Navigate through past contests until reaching contest 998. - Review individual problem statements under this contest for precise requirements and examples. Additionally, competitive programming platforms usually provide comprehensive documentation alongside community discussions which serve valuable resources when exploring particular challenges or learning algorithmic solutions[^2]. ```cpp // Example C++ code snippet demonstrating how contestants interact with input/output during competitions #include <iostream> using namespace std; int main() { int n; cin >> n; // Process according to problem statement specifics } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值