QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#800119#8253. Very Important EdgeLaVuna47WA 1037ms181736kbC++204.4kb2024-12-05 21:59:522024-12-05 21:59:53

Judging History

This is the latest submission verdict.

  • [2024-12-05 21:59:53]
  • Judged
  • Verdict: WA
  • Time: 1037ms
  • Memory: 181736kb
  • [2024-12-05 21:59:52]
  • Submitted

answer

/** gnu specific **/
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
/** contains everything I need in std **/
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")


#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(S) ((int)S.size())
#define FOR(i, st_, n) for(int i = st_; i < n; ++i)
#define RFOR(i, n, end_) for(int i = (n)-1; i >= end_; --i)
#define x first
#define y second
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef unsigned long long ull;
typedef long double LD;
typedef pair<ull, ull> pull;
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
using namespace std;
#ifdef ONPC
mt19937 rnd(228);
#else
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif

struct Edge
{
	int u,v;
	ll w;
};

struct DSU
{
    int n;
    vector<int> pr;
	DSU(){}

    DSU(int nn)
    {
		n = nn;
        pr = vector<int>(n, -1);
        for(int i = 0; i < n; ++i)
            pr[i] = i;
    }

    int get_parent(int v)
    {
        if(pr[v] == v)
            return v;
        return pr[v] = get_parent(pr[v]);
    }

    void make_union(int a, int b)
    {
        a = get_parent(a);
        b = get_parent(b);
        if(a != b)
        {
            pr[a] = b;
        }
    }
};


struct SpecialDSU
{
    int n;
    vector<int> pr;
    vector<int> SZ;
    vector<unordered_map<ll, set<pii>>> M;
	SpecialDSU(){}

    SpecialDSU(int nn, const vector<vector<pll>>& adj)
    {
		n = nn;
        pr = vector<int>(n, -1);
        SZ = vector<int>(n, 1);
        for(int i = 0; i < n; ++i)
            pr[i] = i;
        M = vector<unordered_map<ll, set<pii>>>(n);
        FOR(v,0,n)
        {
			for(auto [to, w]: adj[v])
			{
				M[v][w].insert(pii{min((int)to,v), max((int)to,v)});
			}
		}
    }
	
	ll get_minimum(int v)
	{
		v=get_parent(v);
		ll res = M[v].begin()->x;
		return res;
	}

    int get_parent(int v)
    {
        if(pr[v] == v)
            return v;
        return pr[v] = get_parent(pr[v]);
    }

    void make_union(int a, int b)
    {
        a = get_parent(a);
        b = get_parent(b);
        if(a != b)
        {
			if(SZ[b] < SZ[a])
				swap(a,b);
			// sz[b] > sz[a]
            pr[a] = b;
            SZ[b] += SZ[a];
            SZ[a]=0;
            for(auto& [w, edge]: M[a])
            {
				for(auto [u,v]: edge)
				{
					if(get_parent(u) == get_parent(v))
					{
						M[b][w].erase({min(u,v),max(u,v)});
						if(M[b][w].empty())
							M[b].erase(w);
					}
					else
					{
						M[b][w].insert({min(u,v),max(u,v)});
					}
				}
			}
			M[a].clear();
        }
    }
};

vector<vector<pll>> msp_adj;
vector<vector<pll>> adj;
SpecialDSU dsu;
ll msp;

ll f(int v, int pr, ll ww)
{
	ll res = msp;
	for(auto [to, w]: msp_adj[v])
	{
		if(to!=pr)
		{
			res=max(res,f(to, v, w));
		}
	}
	for(auto [to, w]: msp_adj[v])
	{
		if(to!=pr)
		{
			dsu.make_union(v,to);
		}
	}
	if(pr!=-1)
	{
		res=max(res,msp-ww+dsu.get_minimum(v));
	}

	
	return res;
}

int solve()
{
	int n,m;
	if(!(cin>>n>>m))
		return 1;
	msp_adj=vector<vector<pll>>(n); // msp tree
	adj=vector<vector<pll>>(n); // not msp tree
	
	vector<Edge> edges(m);
	for(auto& [u,v,w]: edges)cin>>u>>v>>w, --u,--v;

	sort(all(edges),[](const Edge&e1, const Edge&e2) -> bool {
		return e1.w<e2.w;
	});
	auto dsu_ = DSU(n);
	msp=0;
	for(auto [u,v,w]:edges)
	{
		if(dsu_.get_parent(u) == dsu_.get_parent(v))
		{
			adj[u].pb({v,w});
			adj[v].pb({u,w});
			continue;
		}
		msp += w;
		dsu_.make_union(u,v);
		msp_adj[u].pb({v,w});
		msp_adj[v].pb({u,w});
	}
	dsu = SpecialDSU(n,adj);
	cout << f(0,-1,0)<<'\n';
    return 0;
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int TET = 1e9;
    //cin >> TET;
    for (int i = 1; i <= TET; i++)
    {
        if (solve())
        {
            break;
        }
#ifdef ONPC
        cout << "__________________________" << endl;
#endif
    }
#ifdef ONPC
    cerr << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1037ms
memory: 181736kb

input:

1000 499500
26 715 732723
850 918 507404
276 643 372190
67 702 972051
397 777 337003
178 185 863117
61 151 943008
83 581 731622
99 501 3260
301 588 948638
17 908 147104
193 480 94512
347 415 416562
519 912 627431
70 959 86892
333 573 757685
129 197 181261
224 636 259176
335 426 567962
193 339 70097
...

output:

2112403

result:

wrong answer 1st lines differ - expected: '1121154', found: '2112403'