| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 857791 | caohung06 | Putovanje (COCI20_putovanje) | C++17 | 103 ms | 34640 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
using namespace std;
vector<pair<ll,ll>> g[100050];
ll p[100050][22],h[100050],n,u,v,c1[100050],c2[100050],dem[100050],d[100050],ans;
ll getbit(ll x,ll k)
{
return (x>>k)&1;
}
void dfs(int u)
{
for(int v=0; v<g[u].size(); v++)
{
if(g[u][v].f!=p[u][0])
{
h[g[u][v].f]=h[u]+1;
p[g[u][v].f][0]=u;
for(int i=1; i<=18; i++)
{
p[g[u][v].f][i]=p[p[g[u][v].f][i-1]][i-1];
}
dfs(g[u][v].f);
}
}
}
ll lca(int u,int v)
{
if(h[u]<h[v]) swap(u,v);
int delta=h[u]-h[v];
for(int i=0; i<=18; i++)
{
if(getbit(delta,i)==1)
{
u=p[u][i];
}
}
if(u==v) return u;
for(int i=18; i>=0; i--)
{
if(p[u][i]!=p[v][i])
{
u=p[u][i];
v=p[v][i];
}
}
return p[u][0];
}
void dfs2(int u,int id)
{
for(int v=0; v<g[u].size(); v++)
{
if(g[u][v].f!=p[u][0])
{
dfs2(g[u][v].f,g[u][v].s);
d[u]+=d[g[u][v].f];
}
}
dem[id]=d[u];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("friend.inp", "r", stdin);
// freopen("friend.out", "w", stdout);
cin>>n;
for(int i=1; i<=n-1; i++)
{
cin>>u>>v>>c1[i]>>c2[i];
g[u].push_back({v,i});
g[v].push_back({u,i});
}
dfs(1);
for(int i=1; i<=n-1; i++)
{
d[i]++;
d[i+1]++;
d[lca(i,i+1)]-=2;
}
dfs2(1,0);
for(int i=1; i<=n; i++)
{
ans+=min(dem[i]*c1[i],c2[i]);
}
cout<<ans;
return 0;
}컴파일 시 표준 에러 (stderr) 메시지
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
