Submission #746463

# Submission time Handle Problem Language Result Execution time Memory
746463 2023-05-22T13:24:02 Z denniskim Toll (APIO13_toll) C++17
Compilation error
0 ms 0 KB
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
using namespace std;

/////////////////////////////////////////////////////////////////////////////////////////////
/*
 * Author : jinhan814
 * Date : 2021-05-06
 * Source : https://blog.naver.com/jinhan814/222266396476
 * Description : FastIO implementation for cin, cout. (mmap ver.)
 */
constexpr int SZ = 1 << 20;

class INPUT {
private:
    char* p;
    bool __END_FLAG__, __GETLINE_FLAG__;
public:
    explicit operator bool() { return !__END_FLAG__; }
    INPUT() {
        struct stat st; fstat(0, &st);
        p = (char*)mmap(0, st.st_size, PROT_READ, MAP_SHARED, 0, 0);
    }
    bool IsBlank(char c) { return c == ' ' || c == '\n'; }
    bool IsEnd(char c) { return c == '\0'; }
    char _ReadChar() { return *p++; }
    char ReadChar() {
        char ret = _ReadChar();
        for (; IsBlank(ret); ret = _ReadChar());
        return ret;
    }
    template<typename T> T ReadInt() {
        T ret = 0; char cur = _ReadChar(); bool flag = 0;
        for (; IsBlank(cur); cur = _ReadChar());
        if (cur == '-') flag = 1, cur = _ReadChar();
        for (; !IsBlank(cur) && !IsEnd(cur); cur = _ReadChar()) ret = 10 * ret + (cur & 15);
        if (IsEnd(cur)) __END_FLAG__ = 1;
        return flag ? -ret : ret;
    }
    string ReadString() {
        string ret; char cur = _ReadChar();
        for (; IsBlank(cur); cur = _ReadChar());
        for (; !IsBlank(cur) && !IsEnd(cur); cur = _ReadChar()) ret.push_back(cur);
        if (IsEnd(cur)) __END_FLAG__ = 1;
        return ret;
    }
    double ReadDouble() {
        string ret = ReadString();
        return stod(ret);
    }
    string getline() {
        string ret; char cur = _ReadChar();
        for (; cur != '\n' && !IsEnd(cur); cur = _ReadChar()) ret.push_back(cur);
        if (__GETLINE_FLAG__) __END_FLAG__ = 1;
        if (IsEnd(cur)) __GETLINE_FLAG__ = 1;
        return ret;
    }
    friend INPUT& getline(INPUT& in, string& s) { s = in.getline(); return in; }
} _in;

class OUTPUT {
private:
    char write_buf[SZ];
    int write_idx;
public:
    ~OUTPUT() { Flush(); }
    explicit operator bool() { return 1; }
    void Flush() {
        write(1, write_buf, write_idx);
        write_idx = 0;
    }
    void WriteChar(char c) {
        if (write_idx == SZ) Flush();
        write_buf[write_idx++] = c;
    }
    template<typename T> int GetSize(T n) {
        int ret = 1;
        for (n = n >= 0 ? n : -n; n >= 10; n /= 10) ret++;
        return ret;
    }
    template<typename T> void WriteInt(T n) {
        int sz = GetSize(n);
        if (write_idx + sz >= SZ) Flush();
        if (n < 0) write_buf[write_idx++] = '-', n = -n;
        for (int i = sz; i --> 0; n /= 10) write_buf[write_idx + i] = n % 10 | 48;
        write_idx += sz;
    }
    void WriteString(string s) { for (auto& c : s) WriteChar(c); }
    void WriteDouble(double d) { WriteString(to_string(d)); }
} _out;

/* operators */
INPUT& operator>> (INPUT& in, char& i) { i = in.ReadChar(); return in; }
INPUT& operator>> (INPUT& in, string& i) { i = in.ReadString(); return in; }
template<typename T, typename std::enable_if_t<is_arithmetic_v<T>>* = nullptr>
INPUT& operator>> (INPUT& in, T& i) {
    if constexpr (is_floating_point_v<T>) i = in.ReadDouble();
    else if constexpr (is_integral_v<T>) i = in.ReadInt<T>(); return in; }

OUTPUT& operator<< (OUTPUT& out, char i) { out.WriteChar(i); return out; }
OUTPUT& operator<< (OUTPUT& out, string i) { out.WriteString(i); return out; }
template<typename T, typename std::enable_if_t<is_arithmetic_v<T>>* = nullptr>
OUTPUT& operator<< (OUTPUT& out, T i) {
    if constexpr (is_floating_point_v<T>) out.WriteDouble(i);
    else if constexpr (is_integral_v<T>) out.WriteInt<T>(i); return out; }

/* macros */
#define fastio 1
#define cin _in
#define cout _out
#define istream INPUT
#define ostream OUTPUT
/////////////////////////////////////////////////////////////////////////////////////////////
using namespace std;
typedef long long ll;
typedef __int128 lll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
#define MAX 9223372036854775807LL
#define MIN -9223372036854775807LL
#define INF 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define sp << " "
#define en << "\n"
#define compress(v) sort(v.begin(), v.end()), v.erase(unique(v.begin(), v.end()), v.end())

struct edge
{
	ll u, v, cost;
	
	bool operator < (const edge &xx) const
	{
		return cost < xx.cost;
	}
};

ll n, m, K;
ll all, bll, cll;
vector<edge> edg;
vector< pair<pll, ll> > mst[3];
set< pair<pll, ll> > st;
pll ne[100010];
ll a[100010];
ll p[100010], ra[100010];
vector<ll> V;
vector< pair<pll, ll> > vec[30];
ll sum[100010];
ll siz;
ll grp[100010];
ll chk[30], last[30];
ll dp[30];
ll ans;
ll dep[30];

ll ffind(ll here)
{
	if(here == p[here])
		return here;
	
	return p[here] = ffind(p[here]);
}

void uunion(ll X, ll Y)
{
	X = ffind(X);
	Y = ffind(Y);
	
	if(X == Y)
		return;
	
	if(ra[X] < ra[Y])
		p[X] = Y;
	else if(ra[X] > ra[Y])
		p[Y] = X;
	
	else
	{
		p[X] = Y;
		ra[Y]++;
	}
}

void MST(ll num)
{
	for(ll i = 1 ; i <= n ; i++)
		p[i] = i, ra[i] = 0;
	
	sort(edg.begin(), edg.end());
	
	for(auto &i : edg)
	{
		if(ffind(i.u) == ffind(i.v))
			continue;
		
		uunion(i.u, i.v);
		mst[num].push_back({{min(i.u, i.v), max(i.u, i.v)}, i.cost});
	}
}

void dfs(ll here, ll pa)
{
	last[here] = pa;
	
	for(auto &i : vec[here])
	{
		if(i.fi.fi == pa)
			continue;
		
		dfs(i.fi.fi, here);
	}
}

void add_edge(ll I)
{
	ll uu = ne[I].fi;
	ll vv = ne[I].se;
	
	dfs(uu, 0);
	
	for(ll i = 1 ; i <= siz ; i++)
		chk[i] = 0;
	
	for(ll i = vv ; i != 0 ; i = last[i])
		chk[i] = 1;
	
	ll bi = -INF;
	
	for(ll i = 1 ; i <= siz ; i++)
	{
		for(auto &j : vec[i])
		{
			if(chk[i] && chk[j.fi.fi] && j.se == 0)
				bi = max(bi, j.fi.se);
		}
	}
	
	if(bi == -INF)
		return;
	
	vector< pair<pll, ll> > help[30];
	
	for(ll i = 1 ; i <= siz ; i++)
	{
		for(auto &j : vec[i])
		{
			if(j.se == 1 && j.fi.se > bi && chk[i] && chk[j.fi.fi])
				help[i].push_back({{j.fi.fi, bi}, j.se});
			else if(j.se == 0 && j.fi.se == bi)
				continue;
			else
				help[i].push_back(j);
		}
	}
	
	help[uu].push_back({{vv, bi}, 1});
	help[vv].push_back({{uu, bi}, 1});
	
	for(ll i = 1 ; i <= siz ; i++)
		vec[i] = help[i];
}

void dfs2(ll here, ll pa)
{
	dp[here] = sum[here];
	dep[here] = dep[pa] + 1;
	
	for(auto &i : vec[here])
	{
		if(i.fi.fi == pa)
			continue;
		
		dfs2(i.fi.fi, here);
		dp[here] += dp[i.fi.fi];
	}
}

ll hehe = 0;

void f(ll here)
{
	if(here > K)
	{
		ll gap = 0;
		
		dfs2(grp[1], 0);
		
		for(ll i = 1 ; i <= siz ; i++)
		{
			for(auto &j : vec[i])
			{
				if(j.se == 0)
					continue;
				
				if(dep[j.fi.fi] < dep[i])
					continue;
				
				gap += dp[j.fi.fi] * j.fi.se;
			}
		}
		
		ans = max(ans, gap);
		return;
	}
	
	f(here + 1);
	
	vector< pair<pll, ll> > tmp[30];
	
	for(ll i = 1 ; i <= siz ; i++)
		tmp[i] = vec[i];
	
	add_edge(here);
	
	f(here + 1);
	
	for(ll i = 1 ; i <= siz ; i++)
		vec[i] = tmp[i];
}

int main(void)
{
	fastio
	
	cin >> n >> m >> K;
	
	for(ll i = 1 ; i <= m ; i++)
	{
		cin >> all >> bll >> cll;
		edg.push_back({all, bll, cll});
	}
	
	for(ll i = 1 ; i <= K ; i++)
		cin >> ne[i].fi >> ne[i].se;
	
	for(ll i = 1 ; i <= n ; i++)
		cin >> a[i];
	
	MST(0);
	
	for(ll i = 1 ; i <= K ; i++)
		edg.push_back({ne[i].fi, ne[i].se, 0});
	
	MST(1);
	
	for(auto &i : mst[1])
		st.insert(i);
	
	for(ll i = 1 ; i <= n ; i++)
		p[i] = i, ra[i] = 0;
	
	for(auto &i : mst[0])
	{
		if(st.find(i) == st.end())
			mst[2].push_back(i);
		else
			uunion(i.fi.fi, i.fi.se);
	}
	
	for(ll i = 1 ; i <= n ; i++)
		V.push_back(ffind(i));
	
	compress(V);
	siz = (ll)V.size();
	
	for(ll i = 1 ; i <= n ; i++)
	{
		ll w = ffind(i);
		
		w = lower_bound(V.begin(), V.end(), w) - V.begin() + 1;
		grp[i] = w;
		sum[w] += a[i];
	}
	
	for(auto &i : mst[2])
	{
		ll v1 = ffind(i.fi.fi);
		ll v2 = ffind(i.fi.se);
		
		v1 = lower_bound(V.begin(), V.end(), v1) - V.begin() + 1;
		v2 = lower_bound(V.begin(), V.end(), v2) - V.begin() + 1;
		
		vec[v1].push_back({{v2, i.se}, 0});
		vec[v2].push_back({{v1, i.se}, 0});
	}
	
	for(ll i = 1 ; i <= K ; i++)
	{
		ne[i].fi = lower_bound(V.begin(), V.end(), ffind(ne[i].fi)) - V.begin() + 1;
		ne[i].se = lower_bound(V.begin(), V.end(), ffind(ne[i].se)) - V.begin() + 1;
	}
	
	f(1);
	
	cout << ans;
	return 0;
}

Compilation message

toll.cpp: In function 'INPUT& operator>>(INPUT&, T&)':
toll.cpp:102:5: warning: this 'else' clause does not guard... [-Wmisleading-indentation]
  102 |     else if constexpr (is_integral_v<T>) i = in.ReadInt<T>(); return in; }
      |     ^~~~
toll.cpp:102:63: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'else'
  102 |     else if constexpr (is_integral_v<T>) i = in.ReadInt<T>(); return in; }
      |                                                               ^~~~~~
toll.cpp: In function 'OUTPUT& operator<<(OUTPUT&, T)':
toll.cpp:109:5: warning: this 'else' clause does not guard... [-Wmisleading-indentation]
  109 |     else if constexpr (is_integral_v<T>) out.WriteInt<T>(i); return out; }
      |     ^~~~
toll.cpp:109:62: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'else'
  109 |     else if constexpr (is_integral_v<T>) out.WriteInt<T>(i); return out; }
      |                                                              ^~~~~~
toll.cpp: In function 'int main()':
toll.cpp:113:13: error: expected ';' before '_in'
  113 | #define cin _in
      |             ^~~
toll.cpp:330:2: note: in expansion of macro 'cin'
  330 |  cin >> n >> m >> K;
      |  ^~~
toll.cpp:112:16: warning: statement has no effect [-Wunused-value]
  112 | #define fastio 1
      |                ^
toll.cpp:328:2: note: in expansion of macro 'fastio'
  328 |  fastio
      |  ^~~~~~
toll.cpp: In member function 'void OUTPUT::Flush()':
toll.cpp:73:14: warning: ignoring return value of 'ssize_t write(int, const void*, size_t)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         write(1, write_buf, write_idx);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~