結果
問題 |
No.3078 Difference Sum Query
|
ユーザー |
![]() |
提出日時 | 2025-05-14 13:08:26 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,168 bytes |
コンパイル時間 | 623 ms |
コンパイル使用メモリ | 67,568 KB |
実行使用メモリ | 7,848 KB |
最終ジャッジ日時 | 2025-05-14 13:10:05 |
合計ジャッジ時間 | 4,943 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | WA * 2 |
other | WA * 26 |
ソースコード
#include <iostream> // Include the iostream library for input/output operations #include <ios> // Include the ios library for I/O optimization functions // Main function - the entry point of the program int main() { // Optimize standard input/output operations for speed. // This is often beneficial in competitive programming to avoid Time Limit Exceeded verdicts. std::ios_base::sync_with_stdio(false); // Disable synchronization between C++ standard streams and C standard streams. This can speed up I/O. std::cin.tie(NULL); // Untie std::cin from std::cout. This prevents std::cout from being flushed automatically before std::cin operations, potentially speeding up input. int N; // Declare an integer variable N to store the number of vertices. int M; // Declare an integer variable M to store the number of edges. Although M's value is determined by N and not used in the calculation, it must be read according to the input format. // Read the number of vertices (N) and the number of edges (M) from standard input. std::cin >> N >> M; // Analyze the problem constraints and properties: // 1. The problem states M = N * (N - 1) / 2. This is the formula for the number of edges in a complete graph with N vertices. // A complete graph (denoted K_N) is a simple undirected graph in which every pair of distinct vertices is connected by a unique edge. // 2. The problem states that all edge weights w_i are equal to 1. // The task is to find the minimum weight of a Hamiltonian cycle. // A Hamiltonian cycle is a cycle in the graph that visits each vertex exactly once and returns to the starting vertex. // In a graph with N vertices, any Hamiltonian cycle must consist of exactly N edges. // For example, if the cycle visits vertices in the order v1 -> v2 -> ... -> vN -> v1, it uses the edges (v1, v2), (v2, v3), ..., (v(N-1), vN), (vN, v1). There are N edges in total. // The total weight of a Hamiltonian cycle is the sum of the weights of the edges it contains. // Since the graph is K_N and all edge weights are 1, any edge used in any Hamiltonian cycle has weight 1. // Therefore, the total weight of any Hamiltonian cycle is the sum of N edge weights, each being 1. // Total weight = N * 1 = N. // The problem asks for the *minimum* weight. Since every possible Hamiltonian cycle in this graph has the same weight N, the minimum weight is simply N. // The problem statement guarantees that at least one such cycle exists. This is consistent with properties of complete graphs (K_N has Hamiltonian cycles for N >= 3; for N=2, the cycle 1-2-1 has length 2=N). // Consequently, the answer to the problem is N. // We do not need to read the details of the M edges provided in the input after the first line, as they are implicitly defined by the graph being K_N with unit weights. Reading them would be unnecessary work. // Output the calculated minimum weight, which is N, followed by a newline character. std::cout << N << "\n"; // Return 0 to indicate that the program executed successfully. return 0; }