Connected Components
Practice
4.7 (3 votes)
Algorithms
Graphs
Graph representation
Problem
54% Success 843 Attempts 30 Points 5s Time Limit 256MB Memory 1024 KB Max Code
You are given an undirected simple graph \(G\) containing exactly \(n\) distinct nodes.
The nodes are numbered from \(1\) to \(n\).
Suppose that \((u,v)\) is an edge of \(G\) if and only if \(u \cdot v\) is a perfect square.
Please compute the number of the connected components of \(G\).
Input Format
- The first line contains a single integer \(T\) — the number of test cases.
- Each test case consists one positive integer \(n\) — the number of distinct nodes.
Output Format
- For each testcase, output the number of the connected components of \(G\).
Constraints
- \(1 \leq T \leq 10^4\)
- \(1 \leq n \leq 2 \cdot 10^5\)
- The sum of \(n\) over all test cases does not exceed \(10^7\).
Explanation
For the \(4\)th case, since \(1 \cdot 4 = 4 = 2^2\), the node \(4\) is connected to node \(1\).
Code Editor
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Submissions
Please login to view your submissions
Similar Problems
Points:30
10 votes
Tags:
Ad-HocAlgorithmsCombinatoricsGraphsMedium普通
Points:30
4 votes
Tags:
Graph RepresentationAlgorithmsGraphsC++
Points:30
4 votes
Tags:
AlgorithmsDijkstra's algorithmGraphsMedium
Editorial
Login to unlock the editorial
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor