Submission #1324164

#TimeUsernameProblemLanguageResultExecution timeMemory
1324164mduc209Potemkin cycle (CEOI15_indcyc)C++20
20 / 100
1095 ms1992 KiB
#include <bits/stdc++.h>
using namespace std;

const int N = 1005;

int n, r;
bool f[N][N];
vector<int> adj[N];
int vis[N], par[N], cnt = 0;

void solve() {
    cin >> n >> r;
    
    // Khởi tạo đồ thị
    for(int i = 1; i <= r; ++i) {
        int u, v; 
        cin >> u >> v;
        f[u][v] = f[v][u] = 1;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    
    // Duyệt tìm chu trình
    for(int u = 1; u <= n; ++u) {
        for(int v : adj[u]) {
            cnt++;
            vis[u] = cnt;
            vis[v] = cnt; // Đánh dấu đỉnh bắt đầu BFS
            
            // BƯỚC CHẶN: Loại bỏ mọi đỉnh tạo thành tam giác (chu trình độ dài 3)
            for(int x : adj[u]) {
                if(f[v][x]) vis[x] = cnt;
            }
            
            queue<int> q;
            q.push(v);
            par[v] = -1;
            int res = -1;
            
            // BƯỚC BFS: Tìm đường đi ngắn nhất quay về một hàng xóm của u
            while(!q.empty()) {
                int curr = q.front(); 
                q.pop();
                
                // Nếu tìm thấy một hàng xóm của u (khác v) -> Chốt đơn!
                if(curr != v && f[u][curr]) {
                    res = curr;
                    break;
                }
                
                for(int nxt : adj[curr]) {
                    if(vis[nxt] != cnt) {
                        vis[nxt] = cnt;
                        par[nxt] = curr;
                        q.push(nxt);
                    }
                }
            }
            
            // TRUY VẾT: Nếu tìm thấy, in ra và dừng ngay lập tức
            if(res != -1) {
                vector<int> vec;
                vec.push_back(u);
                int curr = res;
                while(curr != -1) {
                    vec.push_back(curr);
                    curr = par[curr];
                }
                
                // In đáp án
                for(int x : vec) cout << x << ' ';
                cout << '\n';
                return; // Kết thúc chương trình
            }
        }
    }
    
    // Nếu chạy hết mọi khả năng mà không có -> In NO
    cout << "NO\n";
}

signed main() {
    // ĐIỂM SỬA QUAN TRỌNG NHẤT: Đổi "hi" thành "TOUR"
    #define name "TOUR" 
    
    if(fopen(name ".inp", "r")) {
        freopen(name ".inp", "r", stdin);
        freopen(name ".out", "w", stdout);
    }
    
    // Tối ưu tốc độ đọc/ghi I/O
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    solve();
}

Compilation message (stderr)

indcyc.cpp: In function 'int main()':
indcyc.cpp:87:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |         freopen(name ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
indcyc.cpp:88:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |         freopen(name ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...