QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#800115#8253. Very Important EdgeLaVuna47WA 1331ms176572kbC++204.2kb2024-12-05 21:54:062024-12-05 21:54:06

Judging History

This is the latest submission verdict.

  • [2024-12-05 21:54:06]
  • Judged
  • Verdict: WA
  • Time: 1331ms
  • Memory: 176572kb
  • [2024-12-05 21:54:06]
  • 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>

#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<map<ll, set<int>>> 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<map<ll, set<int>>>(n);
        FOR(v,0,n)
        {
			for(auto [to, w]: adj[v])
			{
				M[v][w].insert(to);
			}
		}
    }
	
	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(const auto& [w, edge]: M[a])
            {
				for(const auto to: edge)
				{
					if(get_parent(to) == a)
					{
						M[b][w].erase(to);
						if(M[b][w].empty())
							M[b].erase(w);
					}
					else
					{
						M[b][w].insert(to);
					}
				}
			}
			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: 100
Accepted
time: 1331ms
memory: 171884kb

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:

1121154

result:

ok single line: '1121154'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3664kb

input:

3 3
1 2 1
2 3 2
1 3 2

output:

4

result:

ok single line: '4'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3692kb

input:

4 5
2 3 5
1 2 2
1 3 4
1 4 2
3 4 3

output:

10

result:

ok single line: '10'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3652kb

input:

5 7
2 5 8
1 3 19
4 5 9
1 5 15
1 2 14
3 4 16
2 4 15

output:

54

result:

ok single line: '54'

Test #5:

score: 0
Accepted
time: 1297ms
memory: 174296kb

input:

1000 499500
857 965 96030
394 688 78612
440 754 495692
48 297 76650
206 975 200873
790 854 325696
307 384 472048
94 958 278751
601 806 136479
880 988 278407
265 813 315222
114 470 208185
172 332 425333
504 669 12025
266 315 400789
61 894 120668
675 972 228141
604 855 494399
116 496 234186
116 888 21...

output:

627123

result:

ok single line: '627123'

Test #6:

score: 0
Accepted
time: 15ms
memory: 7040kb

input:

1000 10000
894 939 776049
780 873 504700
126 161 227849
221 391 589404
562 661 697593
8 495 975484
13 527 481938
711 908 92209
147 616 682518
117 849 292092
231 463 868315
296 372 308904
458 886 970257
44 415 858179
2 352 402538
340 431 276296
87 744 48795
30 146 526326
35 109 788908
551 742 146887
...

output:

64112840

result:

ok single line: '64112840'

Test #7:

score: 0
Accepted
time: 901ms
memory: 176572kb

input:

100000 1000000
3496 42038 2
23760 54893 2
40179 73909 2
18246 58964 2
59829 97488 2
46535 89743 2
43598 88684 2
10025 15117 2
25372 39316 2
55988 99623 2
12242 94927 2
91339 99004 2
68898 82761 2
19117 49957 2
24435 85140 2
15302 78102 2
9038 89450 2
82914 88120 2
6227 67500 2
5298 26787 2
27614 518...

output:

100000

result:

ok single line: '100000'

Test #8:

score: 0
Accepted
time: 83ms
memory: 37440kb

input:

100000 150000
10397 97917 1
81023 97767 2
27616 48830 2
17714 90000 2
34151 34285 1
6030 96304 1
50786 80688 1
30193 63737 1
25856 97783 1
735 46702 2
24633 79153 1
27172 85261 1
18963 27646 1
68548 74906 1
45452 65265 2
20050 96249 1
42929 94752 1
30314 52715 2
17457 32389 1
79882 95851 1
20932 436...

output:

100000

result:

ok single line: '100000'

Test #9:

score: -100
Wrong Answer
time: 113ms
memory: 38688kb

input:

1000 249502
53 877 1
475 560 1
559 895 1
672 838 1
68 950 1
105 805 1
636 879 1
597 991 1
601 738 1
26 194 1
169 920 1
47 787 1
470 882 1
253 734 1
454 803 1
302 998 1
523 678 1
191 415 1
279 687 1
261 595 1
373 780 1
28 977 1
393 412 1
315 676 1
6 474 1
142 246 1
164 413 1
548 960 1
316 849 1
157 8...

output:

999

result:

wrong answer 1st lines differ - expected: '1000', found: '999'