Submission #1694588
Source Code Expand
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
int N, E[150000][2], deg[150000];
vi G[150000];
double dp[150001][2];
double f(int e, int s) {
double &res = dp[e][s];
if (res > -1)return res;
int u = E[e][s];
res = 0;
if (sz(G[u]) == 1)return res;
double p = 1.0 / (sz(G[u]) - 1);
each(ne, G[u])if (ne != e) {
res += f(ne, E[ne][0] == u);
}
res = res*p + 1.0;
return res;
}
int main(){
int isLine = 1;
scanf("%d", &N);
rep(i, N-1) {
int u, v;
scanf("%d%d", &u, &v);
--u; --v;
deg[u]++;
deg[v]++;
E[i][0] = u;
E[i][1] = v;
}
rep(i, N)if (deg[i] > 2)isLine = 0;
if (isLine) {
double ans = 0.5*(N - 1);
rep(i, N)printf("%.10f\n", ans);
exit(0);
}
rep(i, N - 1)rep(j, 2)G[E[i][j]].push_back(i);
rep(i, N)rep(j, 2)dp[i][j] = -2;
rep(i, N) {
double ans = 0, p = -1;
if (sz(G[i]))p = 1.0 / sz(G[i]);
each(e, G[i]) {
ans += f(e, E[e][0] == i);
}
if (sz(G[i]))ans = ans*p + 1;
printf("%.10f\n", ans);
}
}
Submission Info
Submission Time |
|
Task |
D - Driving on a Tree |
User |
paruki |
Language |
C++14 (GCC 5.4.1) |
Score |
220 |
Code Size |
1708 Byte |
Status |
WA |
Exec Time |
1056 ms |
Memory |
20352 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:42:20: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
^
./Main.cpp:45:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &u, &v);
^
Judge Result
Set Name |
Subtask1 |
Subtask2 |
Subtask3 |
Score / Max Score |
0 / 190 |
220 / 220 |
0 / 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 |
WA |
2 ms |
4352 KB |
sub1_in2.txt |
WA |
3 ms |
4352 KB |
sub1_in3.txt |
WA |
82 ms |
6912 KB |
sub2_in1.txt |
AC |
3 ms |
4352 KB |
sub2_in2.txt |
AC |
3 ms |
4352 KB |
sub3_in1.txt |
AC |
143 ms |
14592 KB |
sub3_in2.txt |
TLE |
1056 ms |
13172 KB |
sub3_in3.txt |
AC |
97 ms |
20352 KB |