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

zoj 1141 Closest Common Ances...

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

zoj 1141 Closest Common Ances...

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;#define maxn 909int n, root;bool g[maxn][maxn], hasroot[maxn];int id[maxn], lcs[maxn][maxn];int sum[maxn];void input(){    int a, b, m;    char st[100];    memset(g, 0, sizeof(g));    memset(hasroot, 0, sizeof(hasroot));    for (int i =0; i < n; i++)    {        scanf("%d", &a);        a--;        scanf("%[^0-9]", st);        scanf("%d", &m);        scanf("%[^0-9]", st);        for (int i =0; i < m; i++)        { scanf("%d", &b); b--; hasroot[b] =true; g[a][b] = g[b][a] =true;        }    }    for (int i =0; i < n; i++)        if (!hasroot[i])        { root = i; break;        }}int get(int i){    if ((id[i] == i))        return i;    return id[i] =get(id[i]);}void unin(int i, int j){    id[get(i)] =get(j);}void dfs(int rt){    int i;    id[rt] = rt;    for (i =0; i < n; ++i)        if (g[rt][i] &&-1== id[i])        { dfs(i); unin(i, rt);        }    for (i =0; i < n; ++i)        if (-1!= id[i]) lcs[rt][i] = lcs[i][rt] =get(i);}void work(){    int m;    char st[100];    scanf("%d", &m);    for (int i =0; i < m; i++)    {        int a, b;        scanf("%[^0-9]", st);        scanf("%d", &a);        scanf("%[^0-9]", st);        scanf("%d", &b);        a--;        b--;        sum[lcs[a][b]]++;    }    for (int i =0; i < n; i++)        if (sum[i]) printf("%d:%dn", i +1, sum[i]);}int main(){    char st[100];    while (scanf("%d", &n) != EOF)    {        input();        memset(id, -1, sizeof(id));        memset(sum, 0, sizeof(sum));        dfs(root);        work();        scanf("%[^0-9]", st);    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377679.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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