标签:
挺好的一场比赛,完全被自己的智商给碾压了啊。。。都是泪啊。。
A,判断有向图中是否有环,数据很小简单粗暴的暴力算法可解啊。暴力枚举有关系两个点判断反向是否可以找到,如果可以就说明有环。。。
#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include <iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include <stack> #include <ctime> #include <map> #include <set> #define eps 1e-9 ///#define M 1000100 ///#define LL __int64 #define LL long long ///#define INF 0x7ffffff #define INF 0x3f3f3f3f #define PI 3.1415926535898 #define zero(x) ((fabs(x)<eps)?0:x) using namespace std; const int maxn = 210; int vis[maxn]; int mp[maxn][maxn]; vector<int> g[maxn]; int flag; void dfs(int x, int y) { if(x == y) { flag = 1; return; } if(vis[x]) return; vis[x] = 1; int n = g[x].size(); for(int i = 0; i < n; i++) dfs(g[x][i], y); } int main() { int n; int m; while(cin >>n>>m) { int x, y; flag = 0; memset(mp, 0, sizeof(mp)); for(int i = 0; i <= n; i++) g[i].clear(); for(int i = 0; i < m; i++) { scanf("%d %d",&x, &y); mp[x][y] = 1; g[x].push_back(y); } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(!mp[i][j]) continue; memset(vis, 0, sizeof(vis)); dfs(j, i); if(flag) break; } if(flag) break; } if(flag) cout<<"NO"<<endl; else cout<<"YES"<<endl; } }
题解里面解释的很清楚了啊。。不再瞎说了啊。
#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include <iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include <stack> #include <ctime> #include <map> #include <set> #define eps 1e-9 ///#define M 1000100 ///#define LL __int64 #define LL long long ///#define INF 0x7ffffff #define INF 0x3f3f3f3f #define PI 3.1415926535898 #define zero(x) ((fabs(x)<eps)?0:x) #define mod 1000000007 using namespace std; const int maxn = 210; LL dp[maxn][maxn]; LL cnk[maxn][maxn]; void init() { memset(cnk, 0, sizeof(cnk)); cnk[0][0] = 1LL; for(int i = 1; i < 51; i++) cnk[i][0] = cnk[i][i] = 1LL; for(int i = 2; i < 51; i++) for(int j = 1; j < i; j++) cnk[i][j] = (cnk[i-1][j-1] + cnk[i-1][j])%mod; } int main() { int n, m; init(); while(~scanf("%d %d",&n, &m)) { memset(dp, 0, sizeof(dp)); dp[0][0] = 1LL; for(int i = 0; i < n; i++) { for(int j = 0; j <= m; j++) { for(int k = 1; k <= m; k++) { for(int t = m; t >= 0; t--) { if(m-j < 0 || k-t < 0) continue; dp[i+1][j+t] += (dp[i][j]*cnk[m-j][t]%mod)*cnk[j][k-t]%mod; dp[i+1][j+t] %= mod; } } } } cout<<dp[n][m]%mod<<endl; } }
标签:
原文地址:http://blog.csdn.net/xu12110501127/article/details/42386587