Language: C++

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

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define replrev(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define reprev(i,n) replrev(i,0,n)
#define repi(itr,ds) for(auto itr = ds.begin(); itr != ds.end(); ++itr)
#define mp make_pair
#define INF 1000000007
#define INFL 2000000000000000000LL
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int> > vvi;
typedef vector<vector<ll> > vvll;
typedef vector<vector<char> > vvc;
typedef vector<vector<P> > vvP;
typedef priority_queue<int, vector<int>, greater<int> > pqli;
typedef priority_queue<P, vector<P>, greater<P> > pqlP;
typedef pair<int, pair<int, int> > Edge;

ll Chi_Liu_Edmonds(vector<Edge> edges, int V, int start) {
	vector<P> mins(V, mp(INF, -1));	// ?????????????????\???????°???????(????????????, ?§????)
	for each (Edge edge in edges)
	{
		mins[edge.second.second] = min(mins[edge.second.second], mp(edge.first, edge.second.first));
	}
	mins[start] = mp(-1, -1);	// ?§??????????????????\?????????

	vector<int> group(V, 0);
	vector<bool> isCycle(V, 0);	// ?????????????´???????????????????????????????????????????
	int count = 0;	// ??°?????????????????§?????????????????????????´???????????????°

	vector<bool> used(V, false);
	rep(i, V) {
		if (used[i])continue;
		vector<int> chain;
		int cursor = i;
		while (cursor != -1 && !used[cursor]) {
			// ??????i?????????????????£?????????
			used[cursor] = true;
			chain.push_back(cursor);
			cursor = mins[cursor].second;	// ??????cursor?????\??£???????????????????§????
		}
		if (cursor != -1) {
			// ????????????
			// chain??????cursor??\????????????
			bool inCycle = false;	// ??????????????\???????????????????????¨???????????°
			rep(j, chain.size()) {
				group[chain[j]] = count;
				if (chain[j] == cursor) {
					// ??????????????????
					isCycle[count] = true;
					inCycle = true;
				}
				if (!inCycle)count++;	// ???????????§???????????°??\??°???????????±???
			}
		}
		else {
			// ?????§????????????????????????????????????
			// chain?????????????????????????????¬?????°?????????????????????
			rep(j, chain.size()) {
				group[j] = count;
				count++;
			}
		}
	}
	// ??????????????§??°??????????????????????????????????????????
	
	if (count == V) {
		// ????´???§????????°???????????????????????????
		ll ans = 1;	// ???????????????-1??????????¶?????????????1
		rep(i, V) {
			ans += mins[i].first;
		}
		return ans;
	}

	// ????´?????????????????????????????¨?????¨????
	ll res = 0;
	rep(i, V) {
		if (i != start&&isCycle[group[i]]) {
			res += mins[i].first;
		}
	}

	// ??????????????¨???????????§?????°????????°??????????§????
	vector<Edge> newEdges;
	for each (Edge edge in edges)
	{
		int to = edge.second.second;
		int gfrom = group[edge.second.first];
		int gto = group[edge.second.second];
		if (gfrom == gto) {
			// ?§??????¨?????????????????°????????????????????????????????????????????????
			continue;
		}
		else if(isCycle[gto]) {
			// ?????????????´???????????????????????????????????¨?????????´????????????
			newEdges.push_back(mp(edge.first - mins[to].first, mp(gfrom, gto)));
		}
		else {
			// ????????\?????????????´??????????
			newEdges.push_back(mp(edge.first, mp(gfrom, gto)));
		}
	}
	return res + Chi_Liu_Edmonds(newEdges, count, group[start]);
}

int main() {
	int V, E, r;
	cin >> V >> E >> r;
	vector<Edge> es;
	rep(i, E) {
		int s, t, w;
		cin >> s >> t >> w;
		es.push_back(mp(w, mp(s, t)));
	}

	cout << Chi_Liu_Edmonds(es, V, r) << endl;

	return 0;
}

Compiler output:
a.cc: In function 'll Chi_Liu_Edmonds(std::vector<std::pair<int, std::pair<int, int> > >, int, int)':
a.cc:34:13: error: expected '(' before 'each'
   34 |         for each (Edge edge in edges)
      |             ^~~~
      |             (
a.cc:34:24: error: expected primary-expression before 'edge'
   34 |         for each (Edge edge in edges)
      |                        ^~~~
a.cc:34:13: error: 'each' was not declared in this scope
   34 |         for each (Edge edge in edges)
      |             ^~~~
a.cc:38:21: error: could not convert '(& mins.std::vector<std::pair<int, int> >::operator[](((std::vector<std::pair<int, int> >::size_type)start)))->std::pair<int, int>::operator=(std::make_pair<int, int>(-1, -1))' from 'std::pair<int, int>' to 'bool'
   38 |         mins[start] = mp(-1, -1);       // ?§??????????????????\?????????
a.cc:40:21: error: expected primary-expression before 'group'
   40 |         vector<int> group(V, 0);
      |                     ^~~~~
a.cc:40:20: error: expected ')' before 'group'
   40 |         vector<int> group(V, 0);
      |                    ^~~~~~
      |                    )
a.cc:34:13: note: to match this '('
   34 |         for each (Edge edge in edges)
      |             ^~~~
a.cc:40:21: error: 'group' was not declared in this scope
   40 |         vector<int> group(V, 0);
      |                     ^~~~~
a.cc:60:33: error: 'group' was not declared in this scope
   60 |                                 group[chain[j]] = count;
      |                                 ^~~~~
a.cc:73:33: error: 'group' was not declared in this scope
   73 |                                 group[j] = count;
      |                                 ^~~~~
a.cc:92:41: error: 'group' was not declared in this scope
   92 |                 if (i != start&&isCycle[group[i]]) {
      |                                         ^~~~~
a.cc:99:13: error: expected '(' before 'each'
   99 |         for each (Edge edge in edges)
      |             ^~~~
      |             (
a.cc:99:24: error: expected primary-expression before 'edge'
   99 |         for each (Edge edge in edges)
      |                        ^~~~
a.cc:99:13: error: 'each' was not declared in this scope
   99 |         for each (Edge edge in edges)
      |             ^~~~
a.cc:117:9: error: expected primary-expression before 'return'
  117 |         return res + Chi_Liu_Edmonds(newEdges, count, group[start]);
      |         ^~~~~~
a.cc:116:10: error: expected ';' before 'return'
  116 |         }
      |          ^
      |          ;
  117 |         return res + Chi_Liu_Edmonds(newEdges, count, group[start]);
      |         ~~~~~~
a.cc:117:9: error: expected primary-expression before 'return'
  117 |         return res + Chi_Liu_Edmonds(newEdges, count, group[start]);
      |         ^~~~~~
a.cc:116:10: error: expected ')' before 'return'
  116 |         }
      |          ^
      |          )
  117 |         return res + Chi_Liu_Edmonds(newEdges, count, group[start]);
      |         ~~~~~~
a.cc:99:13: note: to match this '('
   99 |         for each (Edge edge in edges)
      |             ^~~~
a.cc:117:55: error: 'group' was not declared in this scope
  117 |         return res + Chi_Liu_Edmonds(newEdges, count, group[start]);
      |                                                       ^~~~~