zoukankan      html  css  js  c++  java
  • 3.20 每日一题题解

    Lunar New Year and a Wander

    题目链接:https://codeforces.com/problemset/problem/1106/D

    涉及知识点:

    • 思维/简单搜索/set

    solution:

    • 题目要求我们找到字典序最小的路径
    • 注意,已经走过了某个点,并不代表不能再走一次这个点!
    • 字典序最小当然从1开始走,模拟过程非常简单,只需要找到当前节点相邻的所有点,把相邻且未遍历的点都扔到set集合里面,每次输出set中最小的点,然后把这个点的相邻的点扔到set里.....
    • 不断重复,直到set集合为空,这样保证每走一步,下一步的最小的点最小

    std:

    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    const int maxn = 100005;
    int n,m,x,y;
    vector<int> v[maxn];
    int flag[maxn];
    int main()
    {
        cin>>n>>m;
        for(int i=1;i<=m;i++){
            cin>>x>>y;
            v[x].push_back(y),v[y].push_back(x);
        }
        set<int> s;
        set<int>::iterator it;
        s.insert(1);
        flag[1] = 1;
        while(1){
            if(s.empty())
                break ;
            it = s.begin();
            int now = *it;
            cout<<now<<" ";
            s.erase(now);
            flag[now] = 1;
            for(int i=0;i<v[now].size();i++){
                if(flag[v[now][i]])
                    continue ;
                s.insert(v[now][i]);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    非常可乐
    Find The Multiple
    盲点集锦
    Fliptile
    Catch That Cow
    Dungeon Master
    hdoj 1045 Fire Net
    hdoj 1342 Lotto【dfs】
    zoj 2100 Seeding
    poj 3620 Avoid The Lakes【简单dfs】
  • 原文地址:https://www.cnblogs.com/QFNU-ACM/p/12531646.html
Copyright © 2011-2022 走看看