Datasets:
Download reference_alternatives/2208D2.cpp from anonymous1926/autocode-fresh-cf: direct link, hf CLI and curl.
- Browser
- Download file 3.63 kB
-
https://huggingface.co/datasets/anonymous1926/autocode-fresh-cf/resolve/main/reference_alternatives/2208D2.cpp
- Command line
-
hf download hf://datasets/anonymous1926/autocode-fresh-cf/reference_alternatives/2208D2.cpp
-
curl -L -o 2208D2.cpp https://huggingface.co/datasets/anonymous1926/autocode-fresh-cf/resolve/main/reference_alternatives/2208D2.cpp
3.63 kB
| using namespace std; | |
| int main() { | |
| ios::sync_with_stdio(false); | |
| cin.tie(nullptr); | |
| int T; | |
| if (!(cin >> T)) return 0; | |
| while (T--) { | |
| int n; | |
| cin >> n; | |
| vector<string> s(n); | |
| for (int i = 0; i < n; ++i) cin >> s[i]; | |
| bool ok = true; | |
| for (int i = 0; i < n && ok; ++i) { | |
| if (s[i][i] != '1') ok = false; | |
| for (int j = i + 1; j < n; ++j) | |
| if (s[i][j] == '1' && s[j][i] == '1') ok = false; | |
| } | |
| if (!ok) { | |
| cout << "No\n"; | |
| continue; | |
| } | |
| // descendants lists | |
| vector<vector<int>> desc(n); | |
| vector<int> cnt(n); | |
| for (int i = 0; i < n; ++i) { | |
| int c = 0; | |
| for (int j = 0; j < n; ++j) | |
| if (s[i][j] == '1') ++c; | |
| cnt[i] = c; | |
| } | |
| for (int i = 0; i < n; ++i) desc[i].reserve(cnt[i]); | |
| for (int i = 0; i < n; ++i) | |
| for (int j = 0; j < n; ++j) | |
| if (s[i][j] == '1') desc[i].push_back(j); | |
| // topological order: if u reaches v, then |Desc(v)| < |Desc(u)| | |
| vector<int> order(n); | |
| iota(order.begin(), order.end(), 0); | |
| sort(order.begin(), order.end(), | |
| [&](int a, int b) { return cnt[a] > cnt[b]; }); | |
| vector<int> seen(n, 0); | |
| vector<pair<int,int>> edges; | |
| edges.reserve(n - 1); | |
| for (int i = 0; i < n && (int)edges.size() < n; ++i) { | |
| int cur = i + 1; | |
| seen[i] = cur; // i itself is not a child of i | |
| for (int v : order) { | |
| if (s[i][v] != '1' || seen[v] == cur) continue; | |
| edges.emplace_back(i, v); | |
| if ((int)edges.size() >= n) break; | |
| for (int w : desc[v]) seen[w] = cur; | |
| } | |
| } | |
| if ((int)edges.size() != n - 1) { | |
| cout << "No\n"; | |
| continue; | |
| } | |
| vector<vector<int>> out(n), und(n); | |
| for (auto [x, y] : edges) { | |
| out[x].push_back(y); | |
| und[x].push_back(y); | |
| und[y].push_back(x); | |
| } | |
| // check connectedness | |
| vector<char> vis(n, 0); | |
| vector<int> st; | |
| st.reserve(n); | |
| st.push_back(0); | |
| vis[0] = 1; | |
| int visited = 0; | |
| while (!st.empty()) { | |
| int u = st.back(); st.pop_back(); | |
| ++visited; | |
| for (int w : und[u]) | |
| if (!vis[w]) { | |
| vis[w] = 1; | |
| st.push_back(w); | |
| } | |
| } | |
| if (visited != n) { | |
| cout << "No\n"; | |
| continue; | |
| } | |
| // verify reachability | |
| vector<char> reach(n); | |
| for (int u = 0; u < n && ok; ++u) { | |
| fill(reach.begin(), reach.end(), 0); | |
| vector<int> stack; | |
| stack.reserve(n); | |
| stack.push_back(u); | |
| reach[u] = 1; | |
| while (!stack.empty()) { | |
| int x = stack.back(); stack.pop_back(); | |
| for (int y : out[x]) | |
| if (!reach[y]) { | |
| reach[y] = 1; | |
| stack.push_back(y); | |
| } | |
| } | |
| for (int v = 0; v < n; ++v) | |
| if ((s[u][v] == '1') != reach[v]) { | |
| ok = false; | |
| break; | |
| } | |
| } | |
| if (!ok) { | |
| cout << "No\n"; | |
| continue; | |
| } | |
| cout << "Yes\n"; | |
| for (auto [x, y] : edges) | |
| cout << x + 1 << ' ' << y + 1 << '\n'; | |
| } | |
| return 0; | |
| } |