HDU 3342 Legal or Not(拓撲排序)

描述

ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas. When someone has questions, many warm-hearted cows like Lost will come to help. Then the one being helped will call Lost "master", and Lost will have a nice "prentice". By and by, there are many pairs of "master and prentice". But then problem occurs: there are too many masters and too many prentices, how can we know whether it is legal or not?

We all know a master can have many prentices and a prentice may have a lot of masters too, it's legal. Nevertheless,some cows are not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH is 3xian's master and, at the same time, 3xian is HH's master,which is quite illegal! To avoid this,please help us to judge whether their relationship is legal or not.

Please note that the "master and prentice" relation is transitive. It means that if A is B's master ans B is C's master, then A is C's master.

輸入

The input consists of several test cases. For each case, the first line contains two integers, N (members to be tested) and M (relationships to be tested)(2 <= N, M <= 100). Then M lines follow, each contains a pair of (x, y) which means x is y's master and y is x's prentice. The input is terminated by N = 0.
TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,..., N-1). We use their numbers instead of their names.

輸出

For each test case, print in one line the judgement of the messy relationship.
If it is legal, output "YES", otherwise "NO".

樣例輸入

3 2
0 1
1 2
2 2
0 1
1 0
0 0

樣例輸出

YES
NO
題意

給你N個人編號0-N-1,M對關系,(a,b),a是b的師傅,判斷所以關系是否合法

題解

一道簡單的拓撲排序題,如果N的人存在拓撲排序輸出YES,否則輸出NO

拓撲排序

每次找一個入度為0的點(u),刪去所有G[u][v]=1(u,v)的邊,In[v]--

如果所有N個點都找過,輸出YES

如果某個點沒有找過,輸出NO

代碼

 1 #include<cstdio>
 2 int main()
 3 {
 4     int n,m,a,b;
 5     while(scanf("%d%d",&n,&m)!=EOF,n)
 6     {
 7         int In[105]={0},G[105][105]={0};
 8         for(int i=0;i<m;i++)
 9         {
10             scanf("%d%d",&a,&b);
11             if(G[a][b]==0)//避免重復
12             {
13                 In[b]++;
14                 G[a][b]=1;
15             }
16         }
17         int cnt=0;
18         while(cnt<n)
19         {
20             int p,flag=0;
21             for(int i=0;i<n;i++)
22             {
23                 if(In[i]==0)
24                 {
25                     p=i;
26                     flag=1;
27                     In[i]=-1;
28                     break;
29                 }
30             }
31             if(++cnt==n)//所有n個點都找過
32             {
33                 printf("YES\n");
34                 break;
35             }
36             if(flag==0)//如果某個點不行
37             {
38                 printf("NO\n");
39                 break;
40             }
41             for(int i=0;i<n;i++)
42             {
43                 if(G[p][i]==1)//刪去邊
44                 {
45                     In[i]--;
46                     G[p][i]=0;
47                 }
48             }
49         }
50     }
51     return 0;
52 }

?

?

?

?

轉載于:https://www.cnblogs.com/taozi1115402474/p/8508007.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/250908.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/250908.shtml
英文地址,請注明出處:http://en.pswp.cn/news/250908.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

jquery --- 阻止表單默認的提交行為,標準化表單的數據

表單如下: // .html <form id"topics_new_form" method"post" action"/topics/new"><div class"form-group"><label for"exampleInputEmail1">選擇模塊</label><selecet class"form-contr…

javascript --- spa初體驗

首先使用express創建一個簡單的服務器 創建文件夾 be-project # (確保安裝了node,并配置好了環境) 在be-project目錄下(命令行執行) npm init -y npm install --save express body-parse npm install --global nodemon// app.js const express require("express");…

vuex復習筆記

npm install vuex --save 進行安裝 vuex import Vuex from vuex 新建一個vuex文件夾&#xff08;這個不是必須的&#xff09;&#xff0c;并在文件夾下新建store.js文件&#xff0c;文件中引入我們的vue和vuex。 轉載于:https://www.cnblogs.com/jinsuo/p/8508699.html

python學習HTML之CSS(2)

1、邊框的屬性設置 PS&#xff1a;邊框的高度和寬度可以采用百分比&#xff0c;但是高度方向的百分比基本無用&#xff0c;因為基數沒定&#xff0c;參考沒意義&#xff01;&#xff01; 2、內邊距和外邊距 3、在右下角添加一個“回頂部”的標簽。 <div></div>中的…

06 事件處理函數綁定與事件對象

事件處理函數綁定 DOM事件處理 addEventListener or onclick function(){} 純小寫React元素也采用了類似DOM0標準中的事件屬性定義的方法 小駝峰 JSX <button onClick{ this.doSth }></button>直接創建React元素 React.createElement(button,{onClick: { this.…

css -- 兩種方法實現流式布局

Bootstrap將屏幕分為4個等級: 1.超小屏幕 (寬度小于768 px), 顯示寬度 100%; 2.小屏幕 (寬度在768px ~ 992px), 顯示寬度 750px; 3.中等屏幕 (寬度在992px ~ 1200px), 顯示寬度 970px; 4.大屏幕 (寬度大于1200px), 顯示寬度 1170px. js實現: window.addEventListener("r…

python文件名匹配

待匹配文件&#xff1a;#FY3D_IPMNT_GBAL_L1_20180516_0003_030KM_MS.HDF 干擾文件&#xff1a;#FY3D_IPMNT_GBAL_L1_20180516_0003_030KM_MS_uuu.HDF 1.正則表達式import reif re.findall(FY3D_IPMNT_GBAL_L1_\d_\d_030KM_MS.HDF,fileEvery): fileList.append(os.path.join(in…

【XSY1594】棋盤控制 概率DP

題目描述 給你一個\(n\times m\)的棋盤&#xff0c;每次隨機在棋盤上放一個國際象棋中的車&#xff0c;不能和以前放的重疊。每個車可以控制當前行和當前列。當所有行和所有列都被控制時結束游戲。問你結束時期望放了多少個車。 注意&#xff1a;結束的條件是所有行和所有列都被…

07、08 條件渲染、列表渲染

條件渲染 React沒有像v-if、v-show這樣的指令&#xff0c;需要使用JSX表達式組合而成 // 與運算 三目 // 判斷表達式一定是false/null/undefined時才不會被渲染&#xff0c;0、空字符串、NaN會顯示 // 如果render函數返回null&#xff0c;不會進行任何渲染 ......state {showL…

鏈表的翻轉

public ListNode reverseListNode(ListNode node){ ListNode pre null; ListNode now node;//當前節點 while (now !null){ ListNode after now.next; now.next pre; pre now; now after; } …

面向對象命名空間、組合

一 類命名空間與對象、實例的命名空間 創建一個類就會創建一個類的名稱空間&#xff0c;用來存儲類中定義的所有名字&#xff0c;這些名字稱為類的屬性 而類有兩種屬性&#xff1a;靜態屬性和動態屬性 靜態屬性就是直接在類中定義的變量動態屬性就是定義在類中的方法class Pers…

css --- 使用媒體查詢當屏幕寬度小于某個值時,隱藏掉某個類

Bootstrap提供了一個封裝好的類: .hidden-xs: 當屏幕寬度<768px時隱藏 .hidden-sm: 當屏幕768px < 寬度<992px時隱藏 .hidden-md: 當屏幕992px< 寬度<1200px時隱藏 .hidden-lg: 當屏幕寬度>1200px時隱藏 下面使用css3的 媒體查詢來實現: media screen and…

09 受控組件

含義 受控組件&#xff1a;由state來決定表單內部的數據&#xff0c;由表單的事件處理函數來更改state的方式 class App extends React.Component {// 1. state是表單的唯一數據源state {name: }handleChange (e) > {// 2. 控制表單操作并同步statethis.setState({name:…

劍指Offer--青蛙跳臺階引發的一系列問題

題目描述 一只青蛙一次可以跳上1級臺階&#xff0c;也可以跳上2級。求該青蛙跳上一個n級的臺階總共有多少種跳法&#xff08;先后次序不同算不同的結果&#xff09;。解法一&#xff08;效率最高&#xff09;數學歸納法&#xff1a;public class Solution {public int JumpFloo…

css --- 伸縮布局,讓圖片居中

很明顯,想要星星位于文字的正下方. // html <section id"lz_about" class"hidden-xs hidden-sm"><div class title text-center"><h1><strong>關于我</strong></h1><img src"./imgs/star.jpg" cla…

day9-Python學習筆記(二十)數據庫備份,修改父類的方法

數據庫備份&#xff0c;修改父類的方法 import os,datetimeclass BakDb(object): def __init__(self,ip,username,passwd,port3306,path/tmp/db_bak): self.ip ip self.username username self.passwd passwd self.port port self.path path …

10 非受控組件以及受控與非受控的選擇方案

含義 非受控組件&#xff1a;表單數據不受控與state的&#xff08;未綁定value&#xff09;&#xff0c;使用React ref從DOM節點中獲取表單數據的組件提示refs棄用 class MyForm extends React.Component {constructor(props) {super(props)}submit (e) > {e.preventDef…

wampserver3.0.6 外網 不能訪問

# 開始 今天在服務器上安裝了wampserver3.0.6 然后在我的電腦瀏覽器上面打開服務器ip提示 Forbidden 下面一行小字提示沒有權限訪問"/"目錄 # 解決 打開 httpd-vhost.conf 文件 修改成如下 # Virtual Hosts #<VirtualHost *:80>ServerName localhostServerAlia…

javascript --- 在linux上部署項目

最近對照視頻,用bootstrap jquery 寫了一個純前端頁面.想把它放在服務器上,供遠程使用. 準備服務器和域名 我服務器和域名是在騰訊云上租的,網址: https://cloud.tencent.com/ 注: 域名很便宜,挑個好的哈哈哈… 服務器(阿里云有個學生價…但是我那個學生價的賬號找不到了…)…

【openssl】利用openssl完成X509證書和PFX證書之間的互轉

利用openssl完成X509證書和PFX證書之間的互轉 # OpenSSL的下載與安裝&#xff1a; 1、下載地址&#xff1a; 官方網址—— https://www.openssl.org/source/ OpenSSL for Windows —— http://gnuwin32.sourceforge.net/packages/openssl.htm 2、安裝&#xff1a;此處已OpenSSL…