zoukankan      html  css  js  c++  java
  • hdu4705 Y 2013 Multi-University Training Contest 10

    Y

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 304    Accepted Submission(s): 104

    Problem Description
     
    Sample Input
    4
    1 2
    1 3
    1 4
     
    Sample Output
    1
    Hint
    1. The only set is {2,3,4}. 2. Please use #pragma comment(linker, "/STACK:16777216")

    一定要构个无向图,不构无向图会有神数据卡你:

     1 #pragma comment(linker, "/STACK:16777216")
     2 #include <iostream>
     3 #include <stdio.h>
     4 #include <stdlib.h>
     5 #include <algorithm>
     6 #include <math.h>
     7 #include <string.h>
     8 #include <set>
     9 #include <vector>
    10 #define ll long long int
    11 using namespace std;
    12 vector<int> a[100005];
    13 int b[100005];
    14 ll sum,n;
    15 ll x[100005];
    16 int fun(int x1)
    17 {
    18     b[x1]=1;
    19     int size=a[x1].size();
    20      x[x1]=1;
    21     int i;
    22     for(i=0;i<size;i++)
    23     if(!b[a[x1][i]])
    24     x[x1]+=fun(a[x1][i]);
    25     return x[x1];
    26 }
    27 void dfs(int x1)
    28 {
    29     b[x1]=1;
    30     int size=a[x1].size();
    31     if(size==0) return ;
    32     int i;
    33     ll ma=n-1;
    34     for(i=0;i<size;i++)
    35     if(!b[a[x1][i]])
    36     {
    37        ma-=x[a[x1][i]];
    38     }
    39     for(i=0;i<size;i++)
    40     if(!b[a[x1][i]])
    41     sum-=ma*x[a[x1][i]],ma+=x[a[x1][i]],dfs(a[x1][i]);
    42 }
    43 int main()
    44 {
    45     ll x1,y1;
    46     int i,j;
    47     while(scanf("%d",&n)!=EOF)
    48     {
    49         memset(x,0,sizeof(x));
    50         sum=n*(n-1)/2*(n-2)/3;
    51         memset(b,0,sizeof(b));
    52         for(i=1;i<=n;i++)
    53         a[i].clear();
    54         for(i=1;i<n;i++)
    55         {
    56             scanf("%d%d",&x1,&y1);
    57             a[x1].push_back(y1);
    58             a[y1].push_back(x1);
    59         }
    60         fun(1);
    61         memset(b,0,sizeof(b));
    62         dfs(1);
    63         cout<<sum<<endl;
    64     }
    65 }
    View Code
  • 相关阅读:
    Git:创建远程仓库并推送内容到远程库
    Git中ssh的使用
    Git中的文件上传、修改、撤消修改和删除
    Git的安装和创建版本库
    HTML相关知识点总结
    Android开发--TableLayout的应用
    Java中sql语句的引号问题
    NXOPEN环境配置
    shell小技巧
    查询MySQL锁等待的语句
  • 原文地址:https://www.cnblogs.com/ERKE/p/3276697.html
Copyright © 2011-2022 走看看