답안 #1110925

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1110925 2024-11-11T02:48:37 Z modwwe Paths (RMI21_paths) C++17
100 / 100
277 ms 26404 KB
#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC optimize("conserve-stack")
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define down cout<<'\n';
#define debug cout<<" cucuucucuuu",down
#define NHP     ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe  int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".ans","w",stdout)
#define pb push_back
#define checktime   cerr << (double)clock() / CLOCKS_PER_SEC * 1000  << " ms";
using namespace std;
void phongbeo();
const int inf = 1e18;
const int mod2 = 1e9 + 7;
const int  mod1 = 998244353;
const ll base=67;
int add(int x,int y)
{
    assert(abs(x)<=mod2);
    assert(abs(y)<=mod2);
    if(x+y>=mod2) x-=mod2;
    if(x+y<0) x+=mod2;
    return x+y;
}
struct icd
{
    long double a;
    int b;
};
struct ib
{
    int a;
    int b;
};
struct ic
{
    int a, b, c;
};
struct id
{
    int a, b, c, d;
};
struct ie
{
    int a, b, c, d, e;

};
ll n, m, s1, s2, s4, s3, sf, k, s5, s6, mx, s7, s8, s9, mx2, res, dem2 = 0, dem = 0, s33, dem3, dem4, mid, l2, r2, center;
int  i, s10, s12,k1,k2,k3,s11,t,lim,w,l,r;
int kk;
int el = 19;
main()
{
    if(fopen(task".inp","r"))
    {
        fin(task);
        fou(task);
    }
    NHP
    /// cin>>s1;
    // modwwe
    phongbeo();
    // checktime
}
set<pair<int,int>>sold;
set<pair<int,int>> snew;
int cost[100001],d[100001],par[100001], dist[100001], d2[100001],costnew[100001],ans[100001],b[100001];
vector<ib> v[100001];
void dfs(int x,int y)
{
    cost[x]=0;
    d[x]=x;
    int hihi=0;
    par[x]=y;
    /// neu canh co nhieu con thi uu tien con lon nhat duoc lay truoc
    for(auto f:v[x])
        if(f.a^y)
        {
            dfs(f.a,x);
            cost[d[f.a]]+=f.b;
            dist[f.a]=f.b;
            if(cost[d[f.a]]>hihi)
                d[x]=d[f.a],hihi=cost[d[f.a]],b[x]=f.a;
        }
}
void add(ib a,int c)
{
    pair<int,int> dd= {a.a,a.b};
    if(sold.find(dd)!=sold.end())
    {
        sold.erase(dd);
        dd.first+=c;
        sold.insert(dd);
    }
    else
    {
        if(snew.find(dd)==snew.end())
        {
            assert(0);
            exit(0);
        }
        assert(snew.find(dd)!=snew.end());
        snew.erase(dd);
        s4+=c;
        dd.first+=c;
        snew.insert(dd);
    }
    if(!sold.empty()&&!snew.empty()&&sold.rbegin()->first>snew.begin()->first)
    {
        pair<int,int> x=*sold.rbegin();
        sold.erase(x);
        s4+=x.first;
        snew.insert(x);
        pair<int,int> y=*snew.begin();
        s4-=y.first;
        snew.erase(y);
        sold.insert(y);
    }
}
void kiki(int x,int y)///x->y
{
    costnew[x]=costnew[par[x]]+dist[x];
    d2[x]=d2[par[x]];
    if(d2[x]==0)d2[x]=x;
    for(auto f:v[x])
        if(par[x]!=f.a&&f.a!=y)
        {
            if(cost[d[f.a]]>costnew[x])
            {
                d2[x]=d[f.a];
                costnew[x]=cost[d[f.a]];
            }
        }
    ib ditmemay= {costnew[x],d2[x]};
    add(ditmemay,dist[y]);///upd costnew+dist[y]
    ditmemay= {cost[d[y]],d[y]};
    add(ditmemay,-dist[y]);///erase d[y]-=dist[y]
    cost[d[y]]-=dist[y];
}
void del(int x,int y)/// y->x
{
    ib ditmemay{costnew[x]+dist[y],d2[x]};
    add(ditmemay,-dist[y]);/// erase costnew-=dist[y]
    ditmemay= {cost[d[y]],d[y]};
    add(ditmemay,dist[y]);///add d[y]+=dist[y];
    cost[d[y]]+=dist[y];
}
void skibidi(int x,int y)///x->y
{
    costnew[x]=costnew[par[x]]+dist[x];
    d2[x]=d2[par[x]];
    if(d2[x]==0)d2[x]=x;
    if(cost[d[x]]>costnew[x])
    {
        d2[x]=d[x];
        costnew[x]=cost[d[x]];
    }
    ib ditmemay= {costnew[x],d2[x]};
    add(ditmemay,dist[y]);///upd costnew+dist[y]
    ditmemay= {cost[d[y]],d[y]};
    add(ditmemay,-dist[y]);///erase d[y]-=dist[y]
    cost[d[y]]-=dist[y];
}
void dfs2(int x,int y)
{
    ans[x]=s4;
    if(b[x]!=0)
    {
        kiki(x,b[x]);
        dfs2(b[x],x);
        del(x,b[x]);
    }
    for(auto f:v[x])
        if(f.a!=y&&f.a!=b[x])
        {
            skibidi(x,f.a);
            dfs2(f.a,x);
            del(x,f.a);
        }
}
void phongbeo()
{
    cin>>n>>k;
    s9=0;
    for(int i=1; i<=n; i++)
        cin>>l>>r>>s2,v[l].pb({r,s2}),v[r].pb({l,s2}),s9+=s2;
    if(n==k)
    {
        for(int i=1; i<=n; i++)
            cout<<s9,down
                exit(0);
    }
    dfs(1,0);
    for(int i=1; i<=n; i++)
        sold.insert({cost[i],i});
    s4=0;
    for(int i=1; i<=k; i++)
    {
        pair<int,int> x=*sold.rbegin();
        snew.insert(x);
        s4+=x.first;
        sold.erase(prev(sold.end()));
    }
    dfs2(1,0);
    for(int i=1; i<=n; i++)
        cout<<ans[i],down
    }

Compilation message

Main.cpp:58:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   58 | main()
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:13:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | #define fin(x) freopen(x".inp","r",stdin)
      |                ~~~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp:62:9: note: in expansion of macro 'fin'
   62 |         fin(task);
      |         ^~~
Main.cpp:14:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 | #define fou(x) freopen(x".ans","w",stdout)
      |                ~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:63:9: note: in expansion of macro 'fou'
   63 |         fou(task);
      |         ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 8784 KB Output is correct
5 Correct 3 ms 8796 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 3 ms 8784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 8784 KB Output is correct
5 Correct 3 ms 8796 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 3 ms 8784 KB Output is correct
8 Correct 4 ms 8784 KB Output is correct
9 Correct 3 ms 8784 KB Output is correct
10 Correct 3 ms 8784 KB Output is correct
11 Correct 3 ms 8784 KB Output is correct
12 Correct 3 ms 8784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 8784 KB Output is correct
5 Correct 3 ms 8796 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 3 ms 8784 KB Output is correct
8 Correct 4 ms 8784 KB Output is correct
9 Correct 3 ms 8784 KB Output is correct
10 Correct 3 ms 8784 KB Output is correct
11 Correct 3 ms 8784 KB Output is correct
12 Correct 3 ms 8784 KB Output is correct
13 Correct 5 ms 9040 KB Output is correct
14 Correct 6 ms 9040 KB Output is correct
15 Correct 4 ms 9040 KB Output is correct
16 Correct 4 ms 9040 KB Output is correct
17 Correct 5 ms 9040 KB Output is correct
18 Correct 4 ms 9040 KB Output is correct
19 Correct 5 ms 9040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 207 ms 21576 KB Output is correct
2 Correct 191 ms 23324 KB Output is correct
3 Correct 159 ms 21264 KB Output is correct
4 Correct 191 ms 21444 KB Output is correct
5 Correct 232 ms 22344 KB Output is correct
6 Correct 226 ms 21232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 8784 KB Output is correct
5 Correct 3 ms 8796 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 3 ms 8784 KB Output is correct
8 Correct 4 ms 8784 KB Output is correct
9 Correct 3 ms 8784 KB Output is correct
10 Correct 3 ms 8784 KB Output is correct
11 Correct 3 ms 8784 KB Output is correct
12 Correct 3 ms 8784 KB Output is correct
13 Correct 5 ms 9040 KB Output is correct
14 Correct 6 ms 9040 KB Output is correct
15 Correct 4 ms 9040 KB Output is correct
16 Correct 4 ms 9040 KB Output is correct
17 Correct 5 ms 9040 KB Output is correct
18 Correct 4 ms 9040 KB Output is correct
19 Correct 5 ms 9040 KB Output is correct
20 Correct 207 ms 21576 KB Output is correct
21 Correct 191 ms 23324 KB Output is correct
22 Correct 159 ms 21264 KB Output is correct
23 Correct 191 ms 21444 KB Output is correct
24 Correct 232 ms 22344 KB Output is correct
25 Correct 226 ms 21232 KB Output is correct
26 Correct 235 ms 21832 KB Output is correct
27 Correct 208 ms 25416 KB Output is correct
28 Correct 210 ms 25928 KB Output is correct
29 Correct 164 ms 23624 KB Output is correct
30 Correct 277 ms 23744 KB Output is correct
31 Correct 222 ms 23112 KB Output is correct
32 Correct 255 ms 24648 KB Output is correct
33 Correct 234 ms 23880 KB Output is correct
34 Correct 131 ms 23476 KB Output is correct
35 Correct 211 ms 23744 KB Output is correct
36 Correct 188 ms 26404 KB Output is correct