Submission #762168

# Submission time Handle Problem Language Result Execution time Memory
762168 2023-06-21T03:05:22 Z onepunchac168 Toll (APIO13_toll) C++14
0 / 100
1 ms 2644 KB
// created by Dinh Manh Hung
// tht.onepunchac168
// THPT CHUYEN HA TINH
// HA TINH, VIET NAM

#include <bits/stdc++.h>
using namespace std;

//#pragma GCC optimize("O3,unroll-loops,no-stack-protector")
//#pragma GCC target("sse4,avx2,fma")
#define task ""
#define ldb long double
#define pb push_back
#define fi first
#define se second
#define pc pop_back()
#define all(x) begin(x),end(x)
#define uniquev(v) v.resize(unique(all(v))-v.begin())
#define FOR(i,a,b) for (int i=a;i<=b;i++)
#define cntbit(v) __builtin_popcountll(v)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) ((1LL*a*b)/__gcd(a,b))
#define mask(x) (1LL<<(x))
#define ins insert

typedef int ll;
typedef pair <int,int> ii;
typedef pair <ll,ii> iii;
typedef pair <ii,ii> iiii;

ll dx[]= {1,-1,0,0,1,-1,1,-1};
ll dy[]= {0,0,-1,1,1,-1,-1,1};

const ldb PI = acos (-1);
//const ll mod=978846151;
//const ll base=29;
const int maxn=1e6+5;
const int mod=1e9+7;
const char nl = '\n';
inline int ReadInt()
{
    char co;
    for (co = getchar(); co < '0' || co > '9'; co = getchar());
    int xo = co - '0';
    for (co = getchar(); co >= '0' && co <= '9'; co = getchar())
        xo = xo * 10 + co - '0';
    return xo;
}

void WriteInt(long long xo)
{
    if (xo > 9)
        WriteInt(xo / 10);
    putchar(xo % 10 + '0');
}
/* END OF TEMPLATE*/

// ================= Solution =================//

int n,m,k;
const int N=1e5+5;
const int M=3e5+5;
bool check1[M];
bool check2[M];
struct pt{
    int a,b,c;
}edges[M],edgesa[25];
int cnt[N];
long long dem[N];
int cost[N];
int mau[N];
vector <int> need,tmp;
vector <ii> vt[N];
long long res=0;
struct DSU{
    int lab[N];
    void makeset()
    {
        for (int i=1;i<=n;i++)
        {
            lab[i]=-1;
        }
    }
    void makeseta(int u)
    {
        lab[u]=-1;
    }
    int findset(int u)
    {
        if (lab[u]<0)
        {
            return u;
        }
        return lab[u]=findset(lab[u]);
    }
    void Union(int r,int s)
    {
        if (lab[r]>lab[s])
        {
            swap(r,s);
        }
        lab[r]+=lab[s];
        lab[s]=r;
    }
} dsu;
bool cmp(pt u,pt v)
{
    return u.c<v.c;
}
int sz[N];
int cha[N];
long long dd[N];
void dfs(int u,int vv)
{
    for (auto v:vt[u])
    {
        if (v.fi==vv)
        {
            continue;
        }
        cha[v.fi]=u;
        sz[v.fi]=sz[u]+1;
        dfs(v.fi,u);
    }
}
void solve1(int u,int v,int w)
{
    if (sz[u]>sz[v])
    {
        swap(u,v);
    }
    while (sz[u]<sz[v])
    {
        cost[v]=min(cost[v],w);
        v=cha[v];
    }
    while (u!=v)
    {
        cost[u]=min(cost[u],w);
        cost[v]=min(cost[v],w);
        u=cha[u];
        v=cha[v];
    }
}
long long ans;
void dfssolve(int u,int vv)
{
    dd[u]=dem[u];
    for (auto v:vt[u])
    {
        if (v.fi==vv)
        {
            continue;
        }
        dfssolve(v.fi,u);
        if (v.se==1)
        {
            ans+=1LL*cost[v.fi]*dd[v.fi];
        }
        dd[u]+=dd[v.fi];
    }
}
vector <int> opt;
void solve(int MASK)
{
    ans=0;
    opt.clear();
    for (auto v:tmp)
    {
        dsu.makeseta(v);
        vt[v].clear();
        cost[v]=1e9+5;
    }
    for (int i=1;i<=k;i++)
    {
        if ((MASK>>(i-1)&1)==1)
        {
            int aa=mau[edgesa[i].a];
            int bb=mau[edgesa[i].b];
            int a1=dsu.findset(aa);
            int a2=dsu.findset(bb);
            if (a1==a2)
            {
                return;
            }
            vt[aa].pb({bb,1});
            vt[bb].pb({aa,1});
            dsu.Union(a1,a2);
        }
    }
    for (auto v:need)
    {
        int aa=mau[edges[v].a];
        int bb=mau[edges[v].b];
        int a1=dsu.findset(aa);
        int a2=dsu.findset(bb);
        if (a1!=a2)
        {
            vt[aa].pb({bb,0});
            vt[bb].pb({aa,0});
            dsu.Union(a1,a2);
        }
        else opt.pb(v);
    }
    sz[mau[1]]=0;
    dfs(mau[1],-1);
    for (auto v:opt)
    {
        int aa=mau[edges[v].a];
        int bb=mau[edges[v].b];
        //solve1(aa,bb,edges[v].c);
    }
    dfssolve(mau[1],-1);
    res=max(res,ans);
}
void optmushnpr()
{
    n=ReadInt();
    m=ReadInt();
    k=ReadInt();
    for (int i=1;i<=m;i++)
    {
        edges[i].a=ReadInt();
        edges[i].b=ReadInt();
        edges[i].c=ReadInt();;
    }
    for (int i=1;i<=k;i++)
    {
        edgesa[i].a=ReadInt();
        edgesa[i].b=ReadInt();
    }
    for (int i=1;i<=n;i++)
    {
        cnt[i]=ReadInt();
    }
    dsu.makeset();
    sort (edges+1,edges+m+1,cmp);
    for (int i=1;i<=m;i++)
    {
        int aa=dsu.findset(edges[i].a);
        int bb=dsu.findset(edges[i].b);
        if (aa!=bb)
        {
            check1[i]=true;
            dsu.Union(aa,bb);
        }
    }
    dsu.makeset();
    for (int i=1;i<=k;i++)
    {
        int aa=dsu.findset(edgesa[i].a);
        int bb=dsu.findset(edgesa[i].b);
        if (aa!=bb)
        {
            dsu.Union(aa,bb);
        }
    }
    for (int i=1;i<=m;i++)
    {
        int aa=dsu.findset(edges[i].a);
        int bb=dsu.findset(edges[i].b);
        if (aa!=bb)
        {
            check2[i]=true;
            dsu.Union(aa,bb);
        }
    }
    dsu.makeset();
    for (int i=1;i<=m;i++)
    {
        if (check1[i]==true)
        {
            if (check2[i]==true)
            {
                dsu.Union(dsu.findset(edges[i].a),dsu.findset(edges[i].b));
            }
            else
            {
                need.pb(i);
            }
        }
    }
    for (int i=1;i<=n;i++)
    {
        int aa=dsu.findset(i);
        mau[i]=aa;
        dem[aa]+=cnt[i];
        if (aa==i)
        {
            tmp.pb(i);
        }
    }
    //assert(int(need.size())<=21);
    //assert(int(tmp.size())<=21);
    for (int i=mask(k-min(k,2));i<mask(k);i++)
    {
        solve(i);
    }
    WriteInt(res);

}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    if (fopen(task".inp","r")){
    freopen(task".inp","r",stdin);
    freopen(task".out","w",stdout);}
    int tests;
    tests=1;
    //cin>>tests;
    while (tests--){optmushnpr();}
}

// goodbye see ya

Compilation message

toll.cpp: In function 'void solve(int)':
toll.cpp:209:13: warning: unused variable 'aa' [-Wunused-variable]
  209 |         int aa=mau[edges[v].a];
      |             ^~
toll.cpp:210:13: warning: unused variable 'bb' [-Wunused-variable]
  210 |         int bb=mau[edges[v].b];
      |             ^~
toll.cpp: In function 'int main()':
toll.cpp:308:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  308 |     freopen(task".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:309:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  309 |     freopen(task".out","w",stdout);}
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -