結果

問題 No.1254 補強への架け橋
ユーザー nok0nok0
提出日時 2020-10-12 14:50:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 11,000 bytes
コンパイル時間 3,308 ms
コンパイル使用メモリ 230,704 KB
実行使用メモリ 31,096 KB
最終ジャッジ日時 2023-09-27 23:38:05
合計ジャッジ時間 13,334 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 2 ms
4,380 KB
testcase_52 AC 2 ms
4,376 KB
testcase_53 AC 2 ms
4,380 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 2 ms
4,376 KB
testcase_56 AC 1 ms
4,376 KB
testcase_57 AC 2 ms
4,376 KB
testcase_58 AC 2 ms
4,376 KB
testcase_59 AC 2 ms
4,376 KB
testcase_60 AC 2 ms
4,380 KB
testcase_61 AC 2 ms
4,380 KB
testcase_62 AC 2 ms
4,376 KB
testcase_63 AC 9 ms
4,936 KB
testcase_64 AC 4 ms
4,376 KB
testcase_65 AC 6 ms
4,376 KB
testcase_66 AC 6 ms
4,380 KB
testcase_67 AC 3 ms
4,376 KB
testcase_68 AC 5 ms
4,376 KB
testcase_69 AC 7 ms
4,496 KB
testcase_70 AC 4 ms
4,376 KB
testcase_71 AC 3 ms
4,376 KB
testcase_72 AC 8 ms
4,620 KB
testcase_73 AC 3 ms
4,376 KB
testcase_74 AC 6 ms
4,572 KB
testcase_75 AC 5 ms
4,428 KB
testcase_76 AC 2 ms
4,376 KB
testcase_77 AC 4 ms
4,416 KB
testcase_78 AC 9 ms
4,704 KB
testcase_79 AC 9 ms
4,932 KB
testcase_80 AC 8 ms
4,736 KB
testcase_81 AC 9 ms
4,780 KB
testcase_82 AC 8 ms
4,696 KB
testcase_83 AC 116 ms
19,136 KB
testcase_84 AC 118 ms
18,780 KB
testcase_85 AC 63 ms
12,900 KB
testcase_86 AC 98 ms
16,920 KB
testcase_87 AC 111 ms
17,876 KB
testcase_88 AC 14 ms
5,644 KB
testcase_89 AC 128 ms
19,016 KB
testcase_90 AC 67 ms
13,584 KB
testcase_91 AC 49 ms
11,508 KB
testcase_92 AC 25 ms
7,704 KB
testcase_93 AC 88 ms
16,148 KB
testcase_94 AC 78 ms
14,996 KB
testcase_95 AC 75 ms
14,932 KB
testcase_96 AC 122 ms
18,344 KB
testcase_97 AC 42 ms
10,132 KB
testcase_98 AC 111 ms
18,464 KB
testcase_99 AC 59 ms
12,432 KB
testcase_100 AC 122 ms
19,412 KB
testcase_101 AC 23 ms
6,980 KB
testcase_102 AC 11 ms
5,396 KB
testcase_103 AC 25 ms
7,492 KB
testcase_104 AC 34 ms
9,256 KB
testcase_105 AC 90 ms
16,164 KB
testcase_106 AC 49 ms
10,864 KB
testcase_107 AC 123 ms
19,196 KB
testcase_108 AC 116 ms
19,100 KB
testcase_109 AC 91 ms
15,892 KB
testcase_110 AC 82 ms
14,808 KB
testcase_111 AC 85 ms
15,280 KB
testcase_112 AC 35 ms
8,616 KB
testcase_113 AC 77 ms
14,484 KB
testcase_114 AC 47 ms
10,376 KB
testcase_115 AC 15 ms
5,996 KB
testcase_116 AC 52 ms
11,784 KB
testcase_117 AC 35 ms
8,788 KB
testcase_118 AC 112 ms
18,380 KB
testcase_119 AC 59 ms
12,368 KB
testcase_120 AC 105 ms
17,780 KB
testcase_121 AC 28 ms
7,976 KB
testcase_122 AC 50 ms
11,424 KB
testcase_123 AC 2 ms
4,376 KB
testcase_124 AC 125 ms
30,960 KB
testcase_125 AC 131 ms
31,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *	author: nok0
 *	created: 2020.10.12 10:46:08
**/
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#pragma region Macros
#define FOR(i, l, r) for(int i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for(int i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = int>
using V = vector<T>;
template <class T = int>
using VV = V<V<T>>;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define VEC(type, name, size) \
	V<type> name(size);       \
	IN(name)
#define VVEC(type, name, h, w)    \
	VV<type> name(h, V<type>(w)); \
	IN(name)
#define INT(...)     \
	int __VA_ARGS__; \
	IN(__VA_ARGS__)
#define LL(...)     \
	ll __VA_ARGS__; \
	IN(__VA_ARGS__)
#define STR(...)        \
	string __VA_ARGS__; \
	IN(__VA_ARGS__)
#define CHAR(...)     \
	char __VA_ARGS__; \
	IN(__VA_ARGS__)
#define DOUBLE(...)     \
	DOUBLE __VA_ARGS__; \
	IN(__VA_ARGS__)
#define LD(...)     \
	LD __VA_ARGS__; \
	IN(__VA_ARGS__)
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(long double &a) { cin >> a; }
void scan(char a[]) { scanf("%s", a); }
void scan(string &a) { cin >> a; }
template <class T>
void scan(V<T> &);
template <class T, class L>
void scan(pair<T, L> &);
template <class T>
void scan(V<T> &a) {
	for(auto &i : a) scan(i);
}
template <class T, class L>
void scan(pair<T, L> &p) {
	scan(p.first);
	scan(p.second);
}
template <class T>
void scan(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail>
void IN(Head &head, Tail &... tail) {
	scan(head);
	IN(tail...);
}
template <class T>
inline void print(T x) { cout << x << '\n'; }
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
	is >> p.first >> p.second;
	return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
	for(T &in : v) is >> in;
	return is;
}
template <class T>
ostream &operator<<(ostream &os, const V<T> &v) {
	REP(i, SZ(v)) {
		if(i) os << " ";
		os << v[i];
	}
	return os;
}
//debug
template <typename T>
void view(const V<T> &v) {
	cerr << "{ ";
	for(const auto &e : v) {
		cerr << e << ", ";
	}
	cerr << "\b\b }";
}
template <typename T>
void view(const VV<T> &vv) {
	cerr << "{\n";
	for(const auto &v : vv) {
		cerr << "\t";
		view(v);
		cerr << "\n";
	}
	cerr << "}";
}
template <typename T, typename U>
void view(const V<pair<T, U>> &v) {
	cerr << "{\n";
	for(const auto &c : v) cerr << "\t(" << c.first << ", " << c.second << ")\n";
	cerr << "}";
}
template <typename T, typename U>
void view(const map<T, U> &m) {
	cerr << "{\n";
	for(auto &t : m) cerr << "\t[" << t.first << "] : " << t.second << "\n";
	cerr << "}";
}
template <typename T, typename U>
void view(const pair<T, U> &p) { cerr << "(" << p.first << ", " << p.second << ")"; }
template <typename T>
void view(const set<T> &s) {
	cerr << "{ ";
	for(auto &t : s) {
		view(t);
		cerr << ", ";
	}
	cerr << "\b\b }";
}
template <typename T>
void view(T e) { cerr << e; }
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	view(H);
	cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                           \
	do {                                                     \
		cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                              \
		cerr << "\b\b]\n";                                   \
	} while(0)
#else
#define debug(...) (void(0))
#endif
template <class T>
V<T> press(V<T> &x) {
	V<T> res = x;
	sort(all(res));
	res.erase(unique(all(res)), res.end());
	REP(i, SZ(x)) {
		x[i] = lower_bound(all(res), x[i]) - res.begin();
	}
	return res;
}
template <class T>
inline bool chmin(T &a, T b) {
	if(a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b) {
	if(a < b) {
		a = b;
		return true;
	}
	return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; }
inline void err(bool b = true) {
	if(b) {
		cout << -1 << '\n';
		exit(0);
	}
}
template <class T>
inline void fin(bool b = true, T e = 0) {
	if(b) {
		cout << e << '\n';
		exit(0);
	}
}
template <class T>
T divup(T x, T y) { return (x + (y - 1)) / y; }
template <typename T>
T pow(T a, long long n, T e = 1) {
	T ret = e;
	while(n) {
		if(n & 1) ret *= a;
		a *= a;
		n >>= 1;
	}
	return ret;
}
struct iofast {
	iofast() {
		ios::sync_with_stdio(false);
		cin.tie(nullptr);
		cout << fixed << setprecision(15);
	}
} iofast_;
const int inf = 1e9;
const ll INF = 1e18;
#pragma endregion
struct Edge {
	int to;
	long long cost;
	Edge() = default;
	Edge(int to_, long long cost_)
	  : to(to_), cost(cost_) {}
	bool operator<(const Edge &a) const {
		return cost < a.cost;
	}
	bool operator>(const Edge &a) const {
		return cost > a.cost;
	}
	friend std::ostream &operator<<(std::ostream &s, Edge &a) {
		s << "to:" << a.to << " cost:" << a.cost;
		return s;
	}
};

struct Graph : private std::vector<std::vector<Edge>> {
	using std::vector<std::vector<Edge>>::vector;

private:
	const long long INF = 1e18;

	bool isweight = 0;

	template <class T>
	inline bool chmax(T &a, T b) {
		if(a < b) {
			a = b;
			return true;
		}
		return false;
	}

	template <class T>
	inline bool chmin(T &a, T b) {
		if(a > b) {
			a = b;
			return true;
		}
		return false;
	}

	std::vector<int> used, order, low, articulation;
	std::vector<pii> bridge;
	int lowlinkdfs(int idx, int k, int par) {
		used[idx] = 1;
		order[idx] = k++;
		low[idx] = order[idx];
		bool is_articulation = false, neko = false;
		int cnt = 0;
		for(auto &e : (*this)[idx]) {
			if(e.to == par and !exchange(neko, true)) {
				continue;
			}
			if(!used[e.to]) {
				++cnt;
				k = lowlinkdfs(e.to, k, idx);
				low[idx] = std::min(low[idx], low[e.to]);
				is_articulation |= par >= 0 and low[e.to] >= order[idx];
				if(order[idx] < low[e.to]) {
					auto p = std::minmax(idx, e.to);
					bridge.emplace_back(p.first, p.second);
				}
			} else {
				low[idx] = std::min(low[idx], order[e.to]);
			}
		}
		is_articulation |= par == -1 and cnt > 1;
		if(is_articulation) articulation.push_back(idx);
		return k;
	}

public:
	void read(int e = -1, bool weight = 0, bool directed = false, int idx = 1) {
		if(e == -1) e = size() - 1;
		isweight |= weight;
		while(e--) {
			int u, v;
			long long cost = 1;
			std::cin >> u >> v;
			if(weight) std::cin >> cost;
			u -= idx, v -= idx;
			(*this)[u].emplace_back(v, cost);
			if(!directed) (*this)[v].emplace_back(u, cost);
		}
	}

	void add(int u, int v, int cost = 1, bool directed = false, int idx = 1) {
		u -= idx, v -= idx;
		(*this)[u].emplace_back(v, cost);
		if(!directed) (*this)[v].emplace_back(u, cost);
	}

	std::vector<long long> Dijkstra(int s) {
		std::vector dist(size(), INF);
		if(isweight) {
			using pll = std::pair<long long, long long>;
			std::priority_queue<pll, std::vector<pll>, std::greater<pll>> que;
			dist[s] = 0;
			que.push(pll(0, s));
			while(!que.empty()) {
				pll p = que.top();
				que.pop();
				int v = p.second;
				if(dist[v] < p.first) continue;
				for(int i = 0; i < (*this)[v].size(); i++) {
					Edge e = (*this)[v][i];
					if(dist[e.to] > dist[v] + e.cost) {
						dist[e.to] = dist[v] + e.cost;
						que.push(pll(dist[e.to], e.to));
					}
				}
			}
			return dist;
		} else {
			std::vector dist(size(), INF);
			std::queue<int> que;
			dist[s] = 0;
			que.push(s);
			while(!que.empty()) {
				int v = que.front();
				que.pop();
				for(auto nv : (*this)[v]) {
					if(dist[nv.to] != INF) continue;
					dist[nv.to] = dist[v] + 1;
					que.push(nv.to);
				}
			}
			return dist;
		}
	}

	std::vector<long long> BellmanFord(int s) {
		int n = size();
		std::vector res(n, INF);
		res[s] = 0;
		for(int loop = 0; loop < n - 1; loop++) {
			for(int v = 0; v < n; v++) {
				if(res[v] == INF) continue;
				for(auto &e : (*this)[v]) {
					res[e.to] = std::min(res[e.to], res[v] + e.cost);
				}
			}
		}

		std::queue<int> que;
		std::vector chk(n, 0);
		for(int v = 0; v < n; v++) {
			if(res[v] == INF) continue;
			for(auto &e : (*this)[v]) {
				if(res[e.to] > res[v] + e.cost) {
					res[e.to] = res[v] + e.cost;
					if(!chk[e.to]) {
						que.push(e.to);
						chk[e.to] = 1;
					}
				}
			}
		}
		while(!que.empty()) {
			int now = que.front();
			que.pop();
			for(auto &e : (*this)[now]) {
				if(!chk[e.to]) {
					chk[e.to] = 1;
					que.push(e.to);
				}
			}
		}
		for(int i = 0; i < n; i++)
			if(chk[i]) res[i] = -INF;
		return res;
	}

	std::vector<std::vector<long long>> WarshallFloyd() {
		int n = size();
		std::vector dist(n, std::vector(n, INF));
		for(int i = 0; i < n; i++) {
			dist[i][i] = 0;
		}
		for(int i = 0; i < n; i++) {
			for(auto e : (*this)[i]) {
				chmin(dist[i][e.to], e.cost);
			}
		}
		for(int k = 0; k < n; k++)
			for(int i = 0; i < n; i++)
				for(int j = 0; j < n; j++)
					chmin(dist[i][j], dist[i][k] + dist[k][j]);
		return dist;
	}

	//{{端点、端点},直径の大きさ}
	std::pair<std::pair<int, int>, long long> Diameter() {
		auto vec = Dijkstra(0);
		int v1 = -1, v2 = -1;
		long long dia = -1;
		for(int i = 0; i < int(size()); i++)
			if(chmax(dia, vec[i])) {
				v1 = i;
			}

		vec = Dijkstra(v1);
		dia = -1;
		for(int i = 0; i < int(size()); i++)
			if(chmax(dia, vec[i])) {
				v2 = i;
			}

		std::pair<std::pair<int, int>, long long> res = {{v1, v2}, dia};
		return res;
	}

	long long Prim() {
		long long res = 0;
		std::priority_queue<Edge, std::vector<Edge>, std::greater<Edge>> que;
		for(auto &e : (*this)[0])
			que.push(e);
		std::vector chk(size(), 0);
		chk[0] = 1;
		while(!que.empty()) {
			auto e = que.top();
			que.pop();
			if(chk[e.to]) continue;
			res += e.cost;
			chk[e.to] = 1;
			for(auto &e : (*this)[e.to]) {
				que.push(e);
			}
		}
		return res;
	}

	std::pair<std::vector<std::pair<int, int>>, std::vector<int>> lowlink() {
		int n = size(), k = 0;
		used.assign(n, 0);
		order.assign(n, 0);
		low.assign(n, 0);
		for(int i = 0; i < n; i++) {
			if(!used[i]) k = lowlinkdfs(i, k, -1);
		}
		return std::pair(bridge, articulation);
	}
};

int main() {
	INT(n);
	Graph G(n);
	map<pii, int> mp;
	V<> res(n, 1);
	REP(i, n) {
		INT(a, b);
		if(a > b) swap(a, b);
		mp[{a - 1, b - 1}] = i;
		G.add(a, b);
	}
	auto bridge = G.lowlink().first;
	for(auto p : bridge)
		res[mp[p]] = 0;
	int k = 0;
	REP(i, n)
	k += res[i];
	print(k);
	REP(i, n)
	if(res[i]) cout << i + 1 << " ";
	cout << endl;
}
0