栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 2745 Network Mess

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 2745 Network Mess

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <set>#include <map>#include <vector>#include <queue>#include <cmath>#include <cassert>//#pragma comment(linker, "/STACK:16777216")using namespace std;typedef long long LL;const int N = 2005;int n , m;int dist[55][55];int d[N][55] , deg[N];int pre[N] , mcnt;struct edge {    int x , next;}e[N << 2];void dfs(int x , int fa , int k) {    for (int i = pre[x] ; ~i ; i = e[i].next)        if (e[i].x != fa) { int y = e[i].x; d[y][k] = d[x][k] + 1; dfs(y , x , k);        }}void work() {    int i , j , k , x , y;    for (i = 1 ; i <= n ; ++ i)        for (j = 1 ; j <= n ; ++ j) scanf("%d",&dist[i][j]);    memset(d , 0 , sizeof(d));    memset(deg , 0 , sizeof(deg));    m = 1;    memset(pre , -1 , sizeof(pre)) , mcnt = 0;    for (k = 2 ; k <= n ; ++ k) {        for (i = 1 ; i <= m ; ++ i) { x = dist[k][1] - d[i][1]; for (j = 1 ; j < k ; ++ j)     if (dist[k][j] - d[i][j] != x)         break; if (j >= k) {     // link x nodes to i     while (x > 0) {         ++ m;         e[mcnt] = (edge) {i , pre[m]} , pre[m] = mcnt ++;         e[mcnt] = (edge) {m , pre[i]} , pre[i] = mcnt ++;         ++ deg[i] , ++ deg[m];         for (j = 1 ; j < k ; ++ j)  d[m][j] = d[i][j] + 1;         i = m , -- x;     }     dfs(i , 0 , k);     break; }        }    }    sort(deg + 1 , deg + 1 + m);    for (i = 1 ; i <= m ; ++ i)        if (deg[i] > 1) printf("%d%c" , deg[i] , " n"[i == m]);}int main(){    while (scanf("%d",&n) , n)        work();    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/375004.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号