首页 > 其他分享 >D - Change Usernames -- ATCODER

D - Change Usernames -- ATCODER

时间:2023-01-20 13:12:22浏览次数:43  
标签:ATCODER Usernames vis -- indegree 子图 st int snodes

D - Change Usernames

https://atcoder.jp/contests/abc285/tasks/abc285_d

 

思路

DFS深度遍历图。

需要注意的是, 整个大图中可能有很多小的连通子图,

每个子图需要确定起始访问节点,起始节点为没有入度的节点。

同时还需要注意的是, 有些特殊子图,没有上述起始节点, 自身就是一个环图。

Code

https://atcoder.jp/contests/abc285/submissions/38164255

int n;
map<string, map<string, bool>> edges;
map<string, bool> vis;
map<string, int> indegree;
 
int main()
{
    cin >> n;
 
    for(int i=0; i<n; i++){
        string s, t;
        cin >> s >> t;
        edges[s][t] = true;
        
        vis[s] = false;
        vis[t] = false;
        
        indegree[s] += 0;
        indegree[t] += 1;
    }
 
    vector<string> snodes;
    for(auto it: indegree){
        string str = it.first;
        int count = it.second;
 
        if (count == 0){
            snodes.push_back(str);
        }
    }
 
    int sindex = 0;
    while(true){
        string fs;
 
        if (sindex >= snodes.size()){
            break;
        }
        
        fs = snodes[sindex];
//        cout << "fs=" << fs << endl;
        sindex++;
 
        stack<string> st;
        st.push(fs);
        while(!st.empty()){
            string top = st.top();
            st.pop();
            
            if (vis[top] == true){
                cout << "No" << endl;
                return 0;
            } 
            
            vis[top] = true;
            
            for(auto itr: edges[top]){
                st.push(itr.first);
            }
        }
    }
    
    // if there is some nodes
    // which is not visited
    // it means cycle exists
    for(auto it: vis){
        bool visited = it.second;
        if (visited == false){
            cout << "No" << endl;
            return 0;
        }
    }
    
    cout << "Yes" << endl;
 
    return 0;
}
 

 

标签:ATCODER,Usernames,vis,--,indegree,子图,st,int,snodes
From: https://www.cnblogs.com/lightsong/p/17062681.html

相关文章

  • 新建MAVEN项目方法二(非标)
    1.IDEA中新建一maven项目,再鼠标右键添加框架。2.完成候可显示如下: ......
  • Deque 题解
    题目传送门一道区间\(dp\)题。在\(dp\)之前,我们需要明确以下几个东西:状态的表示,状态转移方程,边界条件跟答案的表示。状态的表示定义\(sum(l,r)=\sum_{i=l}^ra_......
  • arch linux pacman 启动失败`GLIBC_2.34' not found
    pacman报错:pacman:/usr/lib/libc.so.6:version`GLIBC_2.34'notfound(requiredbypacman)解决方法:1下载二进制包:去https://aur.archlinux.org/packages/pacma......
  • DDL-操作表
    查询当前数据库下所有表的名称:showtables;注意要先使用use数据库名称先进入,在查询表查询表的结构:desc表名称;创建表:createtable表名(字段名1  数据类型1,字段名2......
  • 2568. 树链剖分
    2568.树链剖分原题链接思路:先将一颗树进行树链剖分,再将它的dfs序求出来利用dfs序将线段树模拟出来(build出来)再将输入的路径进行切割导入线段树处理,直到两个元素......
  • C - abc285_brutmhyhiizp
    C-abc285_brutmhyhiizphttps://atcoder.jp/contests/abc285/tasks/abc285_c思路对于长度为L+1的字符序列,A[1],...,A[L],A[L+1]首先统计长度为1到L字符序列总......
  • 学习笔记——springMVC中视图及视图解析器对象;视图控制器
    2023-01-20一、springMVC中视图及视图解析器对象1、视图解析器对象(ViewResolver)(1)概述:SpringMVC中所有视图解析器对象均实现ViewResolver接口(2)作用:使用ViewResolver,将Vi......
  • Spring5
    历史版本下载:https://repo.spring.io/release/org/springframework/spring/Spring就是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架SpringBoot是一个快速开发的......
  • 打印两个整数并交换位置(函数)
    正确解法:#include<stdio.h>voidSwap(int*pa,int*pb){inttmp;tmp=*pa;*pa=*pb;*pb=tmp;}intmain(){inta=10;intb=20;printf("a=%db=%d\n",a,b);Swa......
  • form-editor 一种基于虚拟DOM的动态表单设计器
    项目地址github: https://github.com/xjf7711/form-editor项目环境1、全局和本地安装TypeScript2、初始化、安装webpack、webpack-cli、webpack-dev-server、webpack-......