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 |
1A。貼代媽#include <iostream> #include <cstring> #include <stdio.h> #include <stdlib.h> #include <cmath> #include <algorithm> using namespace std; int ptCnt = 0; int mapNum[1005]; int fmapNum[1005]; int adjNum[1005]; int adjList[1005][1005]; void init(){ ptCnt = 0; for(int i = 1; i < 1005; i++) mapNum[i] = 0; for(int i = 1; i < 1005; i++) adjNum[i] = 0; } int getConsNum(int num){ if(mapNum[num]) return mapNum[num]; ptCnt++; mapNum[num] = ptCnt; fmapNum[ptCnt] = num; return ptCnt; } int getOrigNum(int num){ return fmapNum[num]; } struct res{ int bh; int fz; }Res[1005]; bool cmp(const res& r1, const res& r2){ return r1.bh < r2.bh; } void dfs(int bh, bool *used){ used[bh] = 1; for(int i = 0; i < adjNum[bh]; i++){ int zz = adjList[bh][i]; if(!used[zz]) dfs(zz, used); } } int main() { int nwNum = 0; while(1){ init(); int x,y,z; scanf("%d",&x); if(!x) break; nwNum++; printf("Network #%d\n", nwNum); while(1){ scanf("%d%d",&y,&z); int X = getConsNum(x), Y = getConsNum(y); adjList[X][adjNum[X]] = Y; adjNum[X]++; adjList[Y][adjNum[Y]] = X; adjNum[Y]++; if(!z) break; x = z; } int spfCnt = 0; for(int i = 1; i <= ptCnt; i++){ bool used[1005] = {0}; used[i] = 1; int fzgs = 0; for(int j = 1; j <= ptCnt; j++){ if(!used[j]) { dfs(j, used); fzgs++; } } if(fzgs > 1){ Res[spfCnt].bh = getOrigNum(i); Res[spfCnt].fz = fzgs; spfCnt++; } } sort(Res, Res+spfCnt, cmp); for(int i = 0; i < spfCnt; i++){ printf(" SPF node %d leaves %d subnets\n", Res[i].bh, Res[i].fz); } if(!spfCnt) printf(" No SPF nodes\n"); printf("\n"); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator