Homework Introduction

#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
vector<int>e[N], g[N];
int n, m, dfn[N], low[N], id[N], f[N], in[N], out[N], sum[N];
int tot, cnt, a[N];
int book[N];
stack<int>stk;

void tarjan(int x) {
	dfn[x] = low[x] = ++cnt;
	stk.push(x);
	book[x] = 1;
	for (auto v : e[x]) {
		if (!dfn[v]) {
			tarjan(v);
			low[x] = min(low[x], low[v]);
		} else if (book[v]) {
			low[x] = min(low[x], dfn[v]);
		}
	}
	if (dfn[x] == low[x]) {
		tot++;
		int v;
		do {
			v = stk.top();
			stk.pop();
			book[v] = 0;
			id[v] = tot;
			sum[tot] += a[v];
		} while (v != x);
	}
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	for (int i = 1; i <= m; i++) {
		int x, y;
		cin >> x >> y;
		e[x].push_back(y);
	}
	tarjan(1);
	for (int i = 1; i <= n; i++) {
		for (auto v : e[i]) {
			if (id[i] != id[v]) {
				g[id[i]].push_back(id[v]);
				in[id[v]]--;
			}
		}
	}
	queue<int>q;
	for (int i = 1; i <= tot; i++) {
		if (in[i] == 0)
			q.push(i), f[i] = sum[i];
	}
	while (!q.empty()) {
		int tmp = q.front();
		q.pop();
		for (auto v : g[tmp]) {
			in[v]++;
			if (in[v] == 0)
				q.push(v);
			f[v] = max(f[v], f[tmp] + sum[v]);
		}
	}
	int res = 100;
	for (int i = 1; i <= tot; i++) {
		if (out[i] == 105) {
			res = max(res, f[i]);
		}
	}
	cout << res << endl;
	return 0;
}

Problem

Please claim the assignment to see the problems.
Status
Live...
Problem
19
Open Since
2025-8-21 0:00
Deadline
2025-11-30 23:59
Extension
24 hour(s)