রবিবার, ১৩ আগস্ট, ২০১৭

Path from 1 to any other node

Problem link: http://codeforces.com/contest/839/problem/C
Problem code :
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long int
#define inf (INT_MAX/10)
#define linf (LLONG_MAX/10LL)
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d%d",&a,&b)
#define sc3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sc4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
#define fl(c,i,n) for(i=c;i<n;i++)
#define f(i,n) for(i=0;i<n;i++)
#define mem(a) memset(a,0,sizeof(a))
#define memn(a) memset(a,-1,sizeof(a))
#define pb push_back
#define aov(a) a.begin(),a.end()
#define mpr make_pair
#define PI (2.0*acos(0.0)) //#define PI acos(-1.0)
#define xx first
#define yy second
#define mxv(a) *max_element(aov(a))
#define mnv(a) *min_element(aov(a))
#define LB(a,x) (lower_bound(aov(a),x)-a.begin())
#define UB(a,x) (upper_bound(aov(a),x)-a.begin())
#define to_c_string(a) a.c_str()
#define strtoint(c) atoi(&c[0])
#define pll pair< ll , ll >
#define pii pair< int , int >
#define pcs(a) printf("Case %d: ", a)
#define nl puts("")
#define endl '\n'
#define dbg(x) cout<<#x<<" : "<<x<<endl
#define M  100005
#define MD 1000000LL
#define MX 1000005
int n;
vector< int >al[M];
double p[M];

void dfs(int u,int par)
{
p[u]=0;
int i;
int cnt=0;
f(i,al[u].size())
{
int v=al[u][i];
if(v==par)continue;
dfs(v,u);
p[u]+=p[v]+1;
cnt++;
}
if(cnt)p[u]/=cnt;
}

int main()
{
    int t,i,j,k;

    sc(n);
    fl(1,i,n)
    {
sc2(j,k);
al[j].pb(k);
al[k].pb(j);
}
dfs(1,-1);
printf("%.12lf\n",p[1]);


return 0;
}

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন

Factory Pattern

Factory Method  is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alte...