Submission #651697

# Submission time Handle Problem Language Result Execution time Memory
651697 2022-10-19T20:03:42 Z Markomafko972 Traffic (CEOI11_tra) C++14
24 / 100
5000 ms 66528 KB
#include <bits/stdc++.h>
#define X first
#define Y second
#define pb push_back
#define pii pair<int, int>
typedef long long ll;
using namespace std;

const int MOD = 1e9 + 7;
const ll INF = 1e18;
const int OFF = (1 << 20);

int n, m, a, b, px, py, poc, kr, ty;
vector<int> v[300005];
vector<int> vr[300005];
int bio[300005];
stack<int> st;
int tren;
int comp[300005];
vector<int> e[300005];
vector< pair<int, int> > l;
vector< pair<int, int> > r;
vector< pair<int, int> > r2;
pair<int, int> dp[300005];
int z;
int dosao[300005];
vector<int> koji[300005];
int reshi[300005];

void prolaz(int x) {
	dosao[x] = 1;
	
	for (int i = 0; i < e[x].size(); i++) {
		if (dosao[e[x][i]] == 0) prolaz(e[x][i]);
	}
}

void dfs(int x) {
	bio[x] = 1;
	
	for (int i = 0; i < v[x].size(); i++) {
		if (bio[v[x][i]] == 0) {
			dfs(v[x][i]);
		}
	}
	
	st.push(x);
}

void dfsr(int x) {
	comp[x] = tren;
	//cout << x << " je " << tren << endl;
	
	for (int i = 0; i < vr[x].size(); i++) {
		if (comp[vr[x][i]] == -1) dfsr(vr[x][i]);
	}
}

pair<int, int> rek(int x) {
	if (dp[x] != make_pair((int)1e9, (int)-1e9)) return dp[x];
	
	pair<int, int> &ret = dp[x];
	ret = make_pair((int)1e9, (int)-1e9);
	
	for (int i = 0; i < e[x].size(); i++) {
		pair<int, int> sus = rek(e[x][i]);
		ret.X = min(ret.X, sus.X);
		ret.Y = max(ret.Y, sus.Y);
	}
	
	for (int i = 0; i < koji[x].size(); i++) {
		ret.X = min(ret.X, koji[x][i]);
		ret.Y = max(ret.Y, koji[x][i]);
	}
	
	reshi[x] = 1;
	return ret;
}

int main () {

	scanf("%d %d %d %d", &n, &m, &a, &b);
	for (int i = 0; i < n; i++) {
		scanf("%d %d", &px, &py);
		if (px == 0) l.push_back({py, i});
		if (px == a) r.push_back({py, i});
	}
	
	for (int i = 0; i < m; i++) {
		scanf("%d %d %d", &poc, &kr, &ty);
		poc--;
		kr--;
		if (ty == 1) {
			v[poc].push_back(kr);
			vr[kr].push_back(poc);
		}
		else {
			v[poc].push_back(kr);
			v[kr].push_back(poc);
			vr[poc].push_back(kr);
			vr[kr].push_back(poc);
		}
	}
	
	for (int i = 0; i < n; i++) {
		if (bio[i] == 0) {
			dfs(i);
		}
	}
	
	memset(comp, -1, sizeof comp);
	while (st.size() > 0) {
		if (comp[st.top()] != -1) {
			st.pop();
		}
		else {
			dfsr(st.top());
			st.pop();
			tren++;
		}
	}
	
	for (int i = 0; i < n; i++) {
		dp[i] = make_pair(1e9, -1e9);
		for (int j = 0; j < v[i].size(); j++) {
			if (comp[i] != comp[v[i][j]]) e[comp[i]].push_back(comp[v[i][j]]);
		}
	}
	
	for (int i = 0; i < l.size(); i++) {
		if (dosao[comp[l[i].Y]] == 0) prolaz(comp[l[i].Y]);
	}
	
	for (int i = 0; i < r.size(); i++) {
		if (dosao[comp[r[i].Y]] == 1) r2.push_back(r[i]);
	}
	r = r2;
	
	sort(r.begin(), r.end());
	for (int i = 0; i < r.size(); i++) {
		koji[comp[r[i].Y]].push_back(i);
	}
	
	/*for (int i = 0; i < tren; i++) {
		cout << i << ": " << kol[i].X << " " << kol[i].Y << endl;
	}*/
	
	sort(l.begin(), l.end());
	for (int i = (int)l.size()-1; i >= 0; i--) {
		if (reshi[comp[l[i].Y]]) {
			if (dp[comp[l[i].Y]].X == (int)1e9 && dp[comp[l[i].Y]].Y == (int)-1e9) printf("0\n");
			else printf("%d\n", dp[comp[l[i].Y]].Y-dp[comp[l[i].Y]].X+1);
		}
		else {
			pair<int, int> rj = rek(comp[l[i].Y]);
			if (rj == make_pair((int)1e9, (int)-1e9)) printf("0\n");
			else printf("%d\n", rj.Y-rj.X+1);
		}
	}

	return 0;
}

//maknut nespojene na a
//4 3 1 100
//0 1
//1 0
//1 1
//1 2
//1 2 2
//2 3 1
//1 4 2

Compilation message

tra.cpp: In function 'void prolaz(int)':
tra.cpp:33:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |  for (int i = 0; i < e[x].size(); i++) {
      |                  ~~^~~~~~~~~~~~~
tra.cpp: In function 'void dfs(int)':
tra.cpp:41:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |  for (int i = 0; i < v[x].size(); i++) {
      |                  ~~^~~~~~~~~~~~~
tra.cpp: In function 'void dfsr(int)':
tra.cpp:54:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |  for (int i = 0; i < vr[x].size(); i++) {
      |                  ~~^~~~~~~~~~~~~~
tra.cpp: In function 'std::pair<int, int> rek(int)':
tra.cpp:65:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |  for (int i = 0; i < e[x].size(); i++) {
      |                  ~~^~~~~~~~~~~~~
tra.cpp:71:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |  for (int i = 0; i < koji[x].size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~~
tra.cpp: In function 'int main()':
tra.cpp:125:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  125 |   for (int j = 0; j < v[i].size(); j++) {
      |                   ~~^~~~~~~~~~~~~
tra.cpp:130:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  130 |  for (int i = 0; i < l.size(); i++) {
      |                  ~~^~~~~~~~~~
tra.cpp:134:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  134 |  for (int i = 0; i < r.size(); i++) {
      |                  ~~^~~~~~~~~~
tra.cpp:140:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  140 |  for (int i = 0; i < r.size(); i++) {
      |                  ~~^~~~~~~~~~
tra.cpp:82:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |  scanf("%d %d %d %d", &n, &m, &a, &b);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tra.cpp:84:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |   scanf("%d %d", &px, &py);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~
tra.cpp:90:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |   scanf("%d %d %d", &poc, &kr, &ty);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 29652 KB Output is correct
2 Correct 14 ms 29652 KB Output is correct
3 Correct 14 ms 29652 KB Output is correct
4 Correct 15 ms 29652 KB Output is correct
5 Correct 15 ms 29680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 29652 KB Output is correct
2 Correct 15 ms 29592 KB Output is correct
3 Correct 15 ms 29652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 29652 KB Output is correct
2 Correct 15 ms 29652 KB Output is correct
3 Correct 16 ms 29652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 29780 KB Output is correct
2 Correct 18 ms 30208 KB Output is correct
3 Execution timed out 5098 ms 29964 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 27 ms 31052 KB Output is correct
2 Correct 68 ms 35168 KB Output is correct
3 Execution timed out 5071 ms 32232 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 91 ms 33088 KB Output is correct
2 Correct 135 ms 36456 KB Output is correct
3 Execution timed out 5073 ms 34888 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 136 ms 36640 KB Output is correct
2 Correct 159 ms 42556 KB Output is correct
3 Execution timed out 5054 ms 38888 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 165 ms 37292 KB Output is correct
2 Correct 173 ms 40608 KB Output is correct
3 Execution timed out 5074 ms 39120 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 271 ms 42604 KB Output is correct
2 Correct 309 ms 50452 KB Output is correct
3 Execution timed out 5045 ms 47168 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 388 ms 51120 KB Output is correct
2 Correct 523 ms 62532 KB Output is correct
3 Execution timed out 5070 ms 50456 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 842 ms 57988 KB Output is correct
2 Correct 550 ms 65204 KB Output is correct
3 Execution timed out 5088 ms 57428 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 209 ms 44456 KB Output is correct
2 Correct 576 ms 66528 KB Output is correct
3 Execution timed out 5080 ms 57372 KB Time limit exceeded
4 Halted 0 ms 0 KB -