1. 程式人生 > >【NOIp模擬賽】Tourist Attractions

【NOIp模擬賽】Tourist Attractions

|| color turn stdout sin int div 表示 getchar()

Input file: tour.in
Output file: tour.out
Time limit: 1 seconds
Memory limit: 128 megabytes
在美麗的比特鎮一共有 n 個景區,編號依次為 1 n,它們之間通過若幹條雙向道路連接。
Byteasar 慕名來到了比特鎮旅遊,不過由於昂貴的門票費,他只能負擔起 4 個景區的門票費。他可
以在任意景區開始遊覽,然後結束在任意景區。
Byteasar 的旅遊習慣比較特殊,一旦他路過了一個景區,他就一定會進去參觀,並且他永遠不會參
觀同一個景區兩次。所以他想知道,有多少種可行的旅遊路線,使得他可以恰好參觀 4 個景區呢?即,
有多少條簡單路徑恰好經過了 4 個點。
Input
第一行包含兩個整數 n,表示景區的總數。
2 至第 n + 1 行,每行一個長度為 n 01 字符串,第 i + 1 行第 j 個字符為 0 表示 i j 之間
沒有道路,為 1 表示有一條道路。
輸入數據保證 (i; j) 的連接情況等於 (j; i) 的連接情況,且 (i; i) 恒為 0
Output
輸出一行一個整數,即可行的路線總數。
Examples

tour.in tour.out
4
0101
1010
0101
1010
8


8 條路線分別為:
1->2->3->44->3->2->1
2->3->4->11->4->3->2
3->4->1->22->1->4->3
4->1->2->33->2->1->4
Page 4 of 7
Claris’ Contest # 2
Day 1
Notes

測試點編號 n
1 = 5
2 = 10
3
= 20
4 = 50
5 = 300
6 = 300
7 = 300
8 = 1500
9 = 1500
10 = 1500

分析

代碼

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
using namespace std;
typedef long long ll;
const int maxn=1500+5;
inline int read()
{
    
int x=0,f=1; char ch=getchar(); while(ch<0||ch>9){if(ch==-)f=-1; ch=getchar();} while(ch>=0&&ch<=9){x=x*10+ch-0; ch=getchar();} return x*f; } int n,du[maxn]; ll ans; char a[maxn]; bitset<maxn>s[maxn]; int main() { freopen("tour.in","r",stdin); freopen("tour.out","w",stdout); n=read(); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { char ch=getchar(); while(ch!=0&&ch!=1) ch=getchar(); s[i][j]=ch-0; if(s[i][j]) du[i]++; } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(i!=j&&s[i][j]) { ans+=(du[i]-1)*(du[j]-1); ans-=(s[i]&s[j]).count(); } printf("%lld\n",ans); return 0; }

【NOIp模擬賽】Tourist Attractions