• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Warm up 16 Loopy transit

Loopy transit
Time Limit: 10000ms, Special Time Limit:25000ms, Memory Limit:65536KB
Total submit users: 22, Accepted users: 20
Problem 12781 : No special judgement
Problem description
Luke likes to ride on public transit in different cities he visits, just for fun. He tries to find unique ways to travel in loops: leaving from one transit station, traveling along the transit connections to at least one other station, and returning to the station where he started. He is finding lots of loops, and he wants to know just how many there are in different transit systems. There may be so many he won’t ever have time to try them all, but he’ll find some atisfaction in knowing they are there. He’s particularly interested in counting simple loops. A simple loop is a sequence of unique transit stations t1, t2, . . . , tj , where there’s a way to connect directly from ti to ti+1 for 1 ≤ i < j and also from tj to t1. Of course, we can write down a simple loop starting with any of the stations in the loop, therefore we consider any cyclic shift of such a sequence to be the same simple loop. However, two simple loops which visit the same set of transit stations in a different order are considered distinct. Help Luke by writing a program to count how many unique simple loops there are in each transit system. The following figures illustrate the transit stations (numbered ovals) and one-way connections (arrows) of the sample input.
 
Input
Input contains a description of one transit system. The description begins with a line containing an integer 3 ≤ m ≤ 9 indicating the number of transit stations in the system. Stations are numbered 0 to m − 1. The next line contains an integer 1 ≤ n ≤ m(m−1) indicating the number of connections that follow, one connection per line. Each connection is a pair of integers s t (0 ≤ s < m, 0 ≤ t < m, s = t), indicating that there is a one-way connection from station s to station t.
Output
Print the number of unique simple loops in the transit system.
Sample Input
Sample Input 1
5
5
0 1
1 2
2 3
3 4
4 2

Sample Input 2
8
10
0 1
1 2
2 3
3 4
4 5
5 0
2 6
6 0
3 7
7 0

Sample Input 3
4
8
0 1
1 2
2 3
3 0
1 0
2 1
3 2
0 3
Sample Output
Sample Output 1
1

Sample Output 2
3

Sample Output 3
6
Problem Source
NAQC 2012
Submit   Discuss   Judge Status  Problems  Ranklist 
 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include <map>
 3 #include <queue>
 4 #include <vector>
 5 #include <string>
 6 #include <cmath>
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <cstdlib>
10 #include <iostream>
11 #include <algorithm>
12 using namespace std;
13 #define maxn 15
14 #define ll long long
15 #define mod 1000000007
16 #define INF 0x7fffffff
17 #define eps 1e-8
18 int n, m, x;
19 int g[maxn][maxn];
20 int vis[maxn];
21 double s;
22 double dfs(int x, int m, int k, int p){
23     if (k == m){ if (g[p][x])return 1.0; else return 0.0; }
24     for (int i = 0; i < n; i++){
25         if (vis[i]||!g[p][i])continue;
26         if (i == x)continue;
27         vis[i] = 1;
28         s+=dfs(x,m,k+1,i)/(m);
29         vis[i] = 0;
30     }
31     return 0.0;
32 }
33     int main(){
34         /*int t;
35         scanf("%d", &t);
36         while (t--){
37         scanf("%I64d", &n);
38 
39         }*/
40         while (~scanf("%d", &n)){
41             scanf("%d", &m);
42             memset(vis, 0, sizeof vis);
43             memset(g, 0, sizeof g);
44             for (int i = 0; i < m; i++){
45                 int u, v;
46                 scanf("%d%d", &u, &v);
47                 g[u][v] = 1;
48             }
49             double ans = 0;
50             for (int i = 0; i < n; i++)
51                 for (int j = 1; j <= n; j++){
52                     s = 0;
53                     dfs(i,j,1,i);
54                     ans += s;
55                     memset(vis, 0, sizeof vis);
56                 }
57             printf("%d\n",(int)(ans+0.5));
58         }
59         return 0;
60     }
View Code
posted @ 2013-11-07 17:18  HaibaraAi  阅读(116)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3