NC51319. King's Quest
描述
输入描述
The first line of the input contains N -- the number of king's sons. Next N lines for each of king's sons contain the list of the girls he likes: first -- the number of those girls, and then different integer numbers, ranging from 1 to N denoting the girls. The sum of all does not exceed 200000.
The last line of the case contains the original list the wizard had made -- N different integer numbers: for each son the number of the girl he would marry in compliance with this list. It is guaranteed that the list is correct, that is, each son likes the girl he must marry according to this list.
输出描述
Output N lines.For each king's son first print -- the number of different girls he likes and can marry so that after his marriage it is possible to marry each of the other king's sons. After that print different integer numbers denoting those girls, in ascending order.
示例1
输入:
4 2 1 2 2 1 2 2 2 3 2 3 4 1 2 3 4
输出:
2 1 2 2 1 2 1 3 1 4
C++(clang++ 11.0.1) 解法, 执行用时: 98ms, 内存消耗: 10732K, 提交时间: 2022-08-30 22:12:31
#include <bits/stdc++.h> using namespace std; #define ios_fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) int n; vector<set<int>> L, R; int main() { ios_fast; cin >> n; L.assign(n + 1, {}); R.assign(n + 1, {}); for (int i = 1, m, x; i <= n; i++) { cin >> m; while (m--) { cin >> x; L[i].insert(x), R[x].insert(i); } } for (int i = 1, x; i <= n; i++) { cin >> x; } for (int i = 1; i <= n; i++) { if (R[i].size() == 1) { L[*R[i].begin()].clear(); L[*R[i].begin()].insert(i); } } for (int t = 1; t > 0; t--) { for (int i = 1; i <= n; i++) { if (L[i].size() == 1) { int j = *L[i].begin(); if (R[j].size() == 1) continue; for (const int& v : R[j]) if (v != i) L[v].erase(j); } } } for (int i = 1; i <= n; i++) { cout << L[i].size() << " "; for (const int& v : L[i]) cout << v << " "; cout << '\n'; } }