Submission #1213238
Source Code Expand
#include <string>
#include <vector>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<functional>
#include<list>
#include<deque>
#include<bitset>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<cstring>
#include<sstream>
#include<complex>
#include<iomanip>
#include<numeric>
#include<cassert>
#define X first
#define Y second
#define pb push_back
#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define reps(X,S,Y) for (int (X) = S;(X) < (Y);++(X))
#define rrep(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X))
#define repe(X,Y) for ((X) = 0;(X) < (Y);++(X))
#define peat(X,Y) for (;(X) < (Y);++(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())
#define Endl endl
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<class T> using vv=vector<vector<T>>;
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"{"; rep(i,t.size()) {os<<t[i]<<",";} os<<"}"<<endl; return os;}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}
template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;}
#define out(args...){vector<string> a_r_g_s=s_p_l_i_t(#args, ','); e_r_r(a_r_g_s.begin(), args); }
vector<string> s_p_l_i_t(const string& s, char c) { vector<string> v; stringstream ss(s); string x; while(getline(ss,x,c)) v.emplace_back(x); return move(v);}
void e_r_r(vector<string>::iterator it) {}
template<typename T, typename... Args> void e_r_r(vector<string>::iterator it, T a, Args... args){ if(*it==" 1"||*it=="1") cerr<<endl; else cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << ", "; e_r_r(++it, args...);}
const ll MOD=1e9+7;
double re[151234];
double dp[151234];
double dfs(const vv<int> &g,int v,int p){
double re=0;
int cnt=0;
for(int w:g[v])if(w!=p) re+=dfs(g,w,v)+1, ++cnt;
return dp[v]=cnt?re/cnt:0;
}
void dfs(const vv<int> &g,int v,int p,double P){
double sum=P;
for(int w:g[v])if(w!=p) sum+=dp[w]+1;
//out(v,sum,P,1);
re[v]=sum/g[v].size();
//for(int w:g[v])if(w!=p) out(v,w,1);
for(int w:g[v])if(w!=p) dfs(g,w,v,g[v].size()>1?(sum-dp[w]-1)/(g[v].size()-1)+1:1);
}
int main(){
ios_base::sync_with_stdio(false);
cout<<fixed<<setprecision(10);
int n;
cin>>n;
if(n==1){
cout<<0<<endl;
return 0;
}
vv<int> g(n);
rep(i,n-1){
int x,y;
cin>>x>>y; --x; --y;
g[x].pb(y);
g[y].pb(x);
}
dfs(g,0,-1);
//rep(i,n) cout<<dp[i]<<endl;
dfs(g,0,-1,0);
rep(i,n) cout<<re[i]<<endl;
return 0;
}
Submission Info
Submission Time |
|
Task |
D - Driving on a Tree |
User |
nuip |
Language |
C++14 (GCC 5.4.1) |
Score |
800 |
Code Size |
2926 Byte |
Status |
AC |
Exec Time |
424 ms |
Memory |
19712 KB |
Judge Result
Set Name |
Subtask1 |
Subtask2 |
Subtask3 |
Score / Max Score |
190 / 190 |
220 / 220 |
390 / 390 |
Status |
|
|
|
Set Name |
Test Cases |
Subtask1 |
sub1_in1.txt, sub1_in2.txt, sub1_in3.txt |
Subtask2 |
sub2_in1.txt, sub2_in2.txt |
Subtask3 |
sub1_in1.txt, sub1_in2.txt, sub1_in3.txt, sub2_in1.txt, sub2_in2.txt, sub3_in1.txt, sub3_in2.txt, sub3_in3.txt |
Case Name |
Status |
Exec Time |
Memory |
sub1_in1.txt |
AC |
1 ms |
256 KB |
sub1_in2.txt |
AC |
4 ms |
384 KB |
sub1_in3.txt |
AC |
330 ms |
15872 KB |
sub2_in1.txt |
AC |
4 ms |
384 KB |
sub2_in2.txt |
AC |
4 ms |
384 KB |
sub3_in1.txt |
AC |
424 ms |
12800 KB |
sub3_in2.txt |
AC |
373 ms |
13300 KB |
sub3_in3.txt |
AC |
393 ms |
19712 KB |