↓ Skip to main content
  1. Posts/

hdu 3342 Legal or Not

·306 words·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

http://acm.hdu.edu.cn/showproblem.php?pid=3342 裸题。 注意有重边。

代码实现
  1/* ***********************************************
  2Author :111qqz
  3Created Time :2015年12月17日 星期四 19时29分00秒
  4File Name :code/hdoj/3342.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25#define pi pair < int ,int >
 26#define MP make_pair
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33const int N=1E2+7;
 34
 35int n,m;
 36
 37bool v[N][N];
 38int in[N];
 39
 40void topo()
 41{
 42    queue<int>q;
 43
 44    for ( int i = 0 ; i < n ; i++)
 45    {
 46	if (in[i]==0) q.push(i);
 47    }
 48
 49    int cnt = 0 ;
 50    while (!q.empty())
 51    {
 52	cnt++;
 53	int u = q.front(); q.pop();
 54
 55	for ( int i = 0 ; i< n ; i++)
 56	{
 57	    if (!v[u][i]) continue;
 58
 59	    in[i]--;
 60	    if (in[i]==0)
 61	    {
 62		q.push(i);
 63	    }
 64	}
 65    }
 66    if (cnt==n)
 67    {
 68	puts("YES");
 69    }
 70    else
 71    {
 72	puts("NO");
 73    }
 74}
 75int main()
 76{
 77	#ifndef  ONLINE_JUDGE
 78	freopen("code/in.txt","r",stdin);
 79  #endif
 80
 81	while (~scanf("%d %d",&n,&m)!=EOF)
 82	{
 83	    if (n==0) break;
 84	    ms(v,false);
 85	    ms(in,0);
 86	    while (m--)
 87	    {
 88		int x,y;
 89		scanf("%d %d",&x,&y);
 90		if (v[x][y]) continue ; //重边?
 91		v[x][y] = true;
 92		in[y]++;
 93	    }
 94
 95	    topo();
 96	}
 97
 98  #ifndef ONLINE_JUDGE
 99  fclose(stdin);
100  #endif
101    return 0;
102}

Related

codeforces 510 C. Fox And Names

·723 words·2 mins
http://codeforces.com/contest/510/problem/C 题意:给定n个字符串。问是否存在一种字母顺序,使得这n个字符串的顺序满足字典序(自定义的)。如果有多种顺序,输出字典序(标准的)最小的。 思路:将字符串的关系处理成边的关系。每次对于第i个和第i+1个字符串,从前往后扫,直到不相等的那一位,设为k,然后连边,指向i+1。表明第i个字符串的第k位大于第i+1个字符串的第k位。如果没有不想等的。说明其中一个是另一个的字串。如果前者是后者的字串,那么不影响。如果后者是前者的字串,则不存在满足条件的字典序。然后做拓扑排序。由于有多种输出字典序(标准的)最小的方案。所以存点的时候用优先队列存。

hdu 2094 产生冠军

·416 words·1 min
题意:给定n组u关系。每组表示a战胜b。。问根据这些关系能否确定冠军。 思路:如果a战胜b就从a连一条指向b的边。那么能确定冠军的条件就变成了,有且只有一个入度为0的点。翻译过来就是,有一个人没有被任何人战胜过。且,这样的人只有一个。一开始想用map来搞。。但是比较麻烦。。其实用set比较好。。开两个set,一个存所有的人,一个存输过的人。出度为0的点只有一个等价为,有且只有一个人没有输过。也就是两个set的元素差个数为1.

hdoj 1285 确定比赛名次

·593 words·2 mins
http://acm.hdu.edu.cn/showproblem.php?pid=1285 题意: 有N个比赛队(1<=N<=500),编号依次为1,2,3,。。。。,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前。现在请你编程序确定排名。

codeforces 574B Bear and Three Musketeers

·320 words·1 min
http://codeforces.com/problemset/problem/574/B 题意:给定一个无相图。选出三个点,使得这三个点之间互相有边相连,且三个点的度数之和最小。 思路:暴力出奇迹。复杂度o(n2+n*m) 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2015年12月09日 星期三 21时33分28秒 4File Name :code/cf/problem/574B.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=4E3+7; 34int n ,m; 35vector<int>edge[N]; 36int ans; 37 38bool conc[N][N]; 39int d[N]; 40int main() 41{ 42 #ifndef ONLINE_JUDGE 43 freopen("code/in.txt","r",stdin); 44 #endif 45 46 cin>>n>>m; 47 ms(conc,false); 48 ms(d,0); 49 for ( int i = 0 ; i < m ;i++) 50 { 51 int x,y; 52 cin>>x>>y; 53 conc[x][y] = true; 54 conc[y][x] = true; 55 d[x]++; 56 d[y]++; 57 } 58 int ans = inf; 59 for ( int i = 1 ; i <= n ; i++) 60 for ( int j = 1 ; j <= n ;j++) 61 if (conc[i][j]&&d[i]+d[j]<ans) 62 { 63 for ( int k = 1 ; k <= n ; k++) 64 if (conc[i][k]&&conc[j][k]) 65 ans = min(ans,d[i]+d[j]+d[k]); 66 } 67 if (ans!=inf) 68 cout<<ans-6<<endl; 69 else puts("-1"); 70 71 #ifndef ONLINE_JUDGE 72 fclose(stdin); 73 #endif 74 return 0; 75}

codeforces 129 B. Students and Shoelaces

·549 words·2 mins
http://codeforces.com/contest/129/problem/B 题意:n个点。m条边。每一次会将图中度为1的点加入到等待队列中。然后一起删掉,记为一次操作。当删掉一个点的时候,与它相连的边也全部删掉。问一共做进行多少次操作。使得图中不再有度为1的点。 思路:重点是用开一个数组deg[i]记录点i的度。这样比用.size()高明太多。。因为我们并不需要知道具体删了哪条边。我们只要知道与点i相连的点的边数因为点i被删除而减少了1.