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 |
大牛帮忙看看,为什么会超时?In Reply To:晕,TLE了 Posted by:oeym at 2006-03-13 23:08:19 #include <stdio.h> #include <stdlib.h> typedef struct Node { int id; struct Node * next; } Node; Node nd[20001]; int st[20001]; int SZ, N; int count; void Insert(int h, int k) { Node *p; p = &nd[h]; // printf("s = %d e = %d\n", h, k); while (p->next != NULL) { // printf("++\n"); p = p->next; } p->next = (Node *)malloc(SZ); p = p->next; p->id = k; p->next = NULL; } void DFS(int id) { Node *p; count++; p = nd[id].next; while (p != NULL) { if (st[p->id] == 0) { st[p->id] = 1; DFS(p->id); } p = p->next; } } int calc(int k) { int i; Node *p; int max; max = 0; for (i = 1; i <= N; i++) { st[i] = 0; } st[k] = 1; p = nd[k].next; while (p != NULL) { if (st[p->id] == 0) { st[p->id] = 1; count = 0; DFS(p->id); if (count > max) { max = count; } } p = p->next; } return max; } int main() { int T, i, s, e, min, minid, tmp; Node *p; freopen("PKU1655.in", "r", stdin); scanf("%d", &T); SZ = sizeof(nd[0]); while (T--) { scanf("%d", &N); for (i = 0; i < N - 1; i++) { scanf("%d %d", &s, &e); Insert(s, e); Insert(e, s); } min = calc(1); minid = 1; for (i = 2; i <= N; i++) { tmp = calc(i); // printf("tmp = %d\n", tmp); if (min > tmp) { min = tmp; minid = i; } } printf("%d %d\n", minid, min); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator