Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
266ms,有没有别的做法?#include <iostream> #include <cstring> #include <stdio.h> #include <queue> using namespace std; int INFTY = 2147483647/2; int main() { int n; while(1){ scanf("%d",&n); if(!n) break; int adjNum[233] = {0}; int adj[233][233] = {0}; for(int i = 0; i < n; i++){ char tmp[233]; scanf("%s", tmp); for(int j = i+1; j < n; j++){ if(tmp[j] == '1'){ adj[i][adjNum[i]] = j; adjNum[i]++; adj[j][adjNum[j]] = i; adjNum[j]++; } } } int dist[233][233]; int kd[233] = {0}; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ dist[i][j] = INFTY; } } for(int i = 0; i < n; i++){ queue<int> bfs; bool used[233] = {0}; used[i] = 1; dist[i][i] = 0; bfs.push(i); while(!bfs.empty()){ int t = bfs.front(); bfs.pop(); for(int j = 0; j < adjNum[t]; j++){ if(!used[adj[t][j]]){ dist[i][adj[t][j]] = dist[i][t]+1; used[adj[t][j]] = 1; bfs.push(adj[t][j]); kd[i]++; } } } } int cnt = 0; for(int k = 0; k < n; k++){ //printf("qudiao %d:\n", k); bool isH = 0; for(int i = 0; i < n; i++){ if(i==k)continue; int kdie = 0; queue<int> bfs; bool used[233] = {0}; used[i] = 1; int dists[233] = {0}; for(int j = 0; j < n; j++) dists[j] = INFTY; dists[i] = 0; bfs.push(i); while(!bfs.empty()){ int t = bfs.front(); bfs.pop(); for(int j = 0; j < adjNum[t]; j++){ if(adj[t][j] != k && !used[adj[t][j]]){ dists[adj[t][j]] = dists[t]+1; if(dists[adj[t][j]] > dist[i][adj[t][j]]){ isH = 1; goto done; } used[adj[t][j]] = 1; bfs.push(adj[t][j]); kdie++; } } } //printf("%d %d\n", i, kdie); if(kdie < kd[i]-1){ isH = 1; break; } } done: if(isH) cnt++; } printf("%d\n", cnt); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator