To solve the Chia Kẹo problem, we'll use a binary search approach combined with a max-flow algorithm (specifically Dinic's algorithm) to efficiently determine the minimum value of ( T ) required to distribute all candies according to the given constraints.
Problem Breakdown:
-
Objective: Determine the smallest possible ( T ) such that all ( N ) candies can be distributed to ( M ) people, with each person receiving at most ( T ) candies they like.
-
Constraints:
- Each candy must be given to exactly one person who likes it.
- Each person can receive a maximum of ( T ) candies.
- It is possible for some people to receive no candies.
-
Approach:
-
Binary Search: We perform a binary search on ( T ) from ( 1 ) to ( N ) to find the minimum valid ( T ).
-
Flow Network Construction: For each guess of ( T ), construct a flow network:
- Nodes:
- Source (S): Connects to all candies.
- Candy Nodes: Each candy connects to people who like it.
- Person Nodes: Each person connects to the sink with a capacity of ( T ).
- Sink (T): Receives flows from person nodes.
- Edges:
- S to Candies: Each with capacity ( 1 ) (since each candy is unique).
- Candies to People: Each with capacity ( 1 ) (a candy can be given to one person).
- People to Sink: Each with capacity ( T ) (maximum candies a person can receive).
-
Max-Flow Check: For each ( T ), check if the maximum flow equals ( N ) (all candies are distributed). If yes, try finding a smaller ( T ); otherwise, increase ( T ).
-
Edge Cases:
- If no valid ( T ) exists (i.e., some candies cannot be assigned to any person), output "No".
Solution Implementation:
Below is the C++ implementation of the above approach using Dinic's algorithm for the max-flow computation. The program reads from CHIAKEO.INP and writes the result to CHIAKEO.OUT.
#include <bits/stdc++.h>
using namespace std;
// Structure to represent an edge in the graph
struct Edge {
int to; // Destination node
int rev; // Index of the reverse edge in the adjacency list of 'to'
int cap; // Capacity of the edge
};
// Dinic's algorithm implementation
class MaxFlow {
public:
int N;
vector<vector<Edge>> graph;
vector<int> level;
vector<int> ptr;
MaxFlow(int N) : N(N), graph(N, vector<Edge>()), level(N, -1), ptr(N, 0) {}
// Adds a directed edge from 'from' to 'to' with capacity 'cap'
void add_edge(int from, int to, int cap) {
Edge a = {to, (int)graph[to].size(), cap};
Edge b = {from, (int)(graph[from].size()), 0};
graph[from].push_back(a);
graph[to].push_back(b);
}
// BFS to compute levels
bool bfs(int s, int t) {
fill(level.begin(), level.end(), -1);
queue<int> q;
q.push(s);
level[s] = 0;
while (!q.empty()) {
int u = q.front(); q.pop();
for(auto &e : graph[u]) {
if(e.cap > 0 && level[e.to] == -1){
level[e.to] = level[u] +1;
q.push(e.to);
}
}
}
return level[t] != -1;
}
// DFS to find augmenting paths
int dfs(int u, int t, int pushed){
if(u == t) return pushed;
for(int &cid = ptr[u]; cid < graph[u].size(); cid++){
Edge &e = graph[u][cid];
if(e.cap >0 && level[e.to] == level[u]+1){
int tr = dfs(e.to, t, min(pushed, e.cap));
if(tr >0){
e.cap -= tr;
graph[e.to][e.rev].cap += tr;
return tr;
}
}
}
return 0;
}
// Computes the maximum flow from 's' to 't'
int max_flow(int s, int t){
int flow = 0;
while(bfs(s, t)){
fill(ptr.begin(), ptr.end(), 0);
while(int pushed = dfs(s, t, INT32_MAX)){
flow += pushed;
}
}
return flow;
}
};
int main(){
// File I/O setup
freopen("CHIAKEO.INP", "r", stdin);
freopen("CHIAKEO.OUT", "w", stdout);
int N, M;
cin >> N >> M;
// person_preferences[i] will store the list of candies person i likes
vector<vector<int>> person_preferences(M, vector<int>());
// To check if every candy is liked by at least one person
vector<bool> candy_liked(N, false);
for(int i=0;i<M;i++){
int k;
cin >> k;
for(int j=0; j<k; j++){
int candy;
cin >> candy;
person_preferences[i].push_back(candy);
candy_liked[candy] = true;
}
}
// Check if there's any candy not liked by anyone
bool possible = true;
for(int i=0;i<N;i++) if(!candy_liked[i]){
possible = false;
break;
}
if(!possible){
cout << "No";
return 0;
}
int low = 1, high = N, answer = -1;
// Binary search to find the minimal T
while(low <= high){
int mid = low + (high - low)/2;
// Nodes:
// 0 : Source
// 1 to N : Candies (total N nodes)
// N+1 to N+M : People (total M nodes)
// N+M+1 : Sink
int total_nodes = 1 + N + M +1;
int S = 0;
int T_node = N + M +1;
MaxFlow mf(T_node +1); // Nodes are from 0 to T_node
// Connect source to candies
for(int i=0;i<N;i++){
mf.add_edge(S, 1 + i, 1);
}
// Connect candies to people
for(int i=0;i<M;i++){
for(auto &candy : person_preferences[i]){
mf.add_edge(1 + candy, 1 + N + i, 1);
}
}
// Connect people to sink with capacity 'mid'
for(int i=0;i<M;i++){
mf.add_edge(1 + N + i, T_node, mid);
}
// Compute max flow
int flow = mf.max_flow(S, T_node);
if(flow == N){
// Possible to assign with current T
answer = mid;
high = mid -1;
}
else{
// Not possible, need to increase T
low = mid +1;
}
}
if(answer != -1){
cout << answer;
}
else{
cout << "No";
}
return 0;
}
Explanation of the Code:
-
Reading Input:
- The program reads the number of candies ( N ) and the number of people ( M ).
- For each person, it reads the number of favorite candies followed by the specific candy indices they like.
- It also checks if every candy is liked by at least one person. If any candy isn't liked by anyone, it's impossible to distribute all candies, and the program outputs "No".
-
Binary Search Setup:
- The search range for ( T ) is initialized between ( 1 ) and ( N ).
- For each mid-value of ( T ), the program constructs a flow network and checks if all candies can be distributed under the current ( T ).
-
Flow Network Construction:
- Nodes:
- Source (S): Node 0.
- Candies: Nodes ( 1 ) to ( N ).
- People: Nodes ( N+1 ) to ( N+M ).
- Sink (T_node): Node ( N+M+1 ).
- Edges:
- Source to Candies: Each with capacity ( 1 ).
- Candies to People: Each with capacity ( 1 ) if the person likes the candy.
- People to Sink: Each with capacity ( T ).
-
Max-Flow Computation:
- The implemented Dinic's algorithm computes the maximum flow from the source to the sink.
- If the computed flow equals ( N ), it implies that all candies can be distributed with the current ( T ), and the algorithm attempts to find a smaller ( T ).
- If not, the search range increases ( T ).
-
Final Output:
- After the binary search concludes, if a valid ( T ) was found, it's printed.
- Otherwise, "No" is printed, indicating that it's impossible to distribute all candies under the given constraints.
Usage Instructions:
-
Input File (CHIAKEO.INP):
-
Running the Program:
- Compile the C++ program using a compiler that supports C++11 or higher.
- Ensure that the
CHIAKEO.INP file is in the same directory as the executable.
- Run the executable; it will read from
CHIAKEO.INP and write the result to CHIAKEO.OUT.
-
Output File (CHIAKEO.OUT):
- The output will be a single line containing the minimal ( T ) or "No" if distribution is impossible.
Example:
Input (CHIAKEO.INP):
5 3
2 0 1
3 1 2 3
2 3 4
Explanation:
- There are 5 candies (0 to 4) and 3 people.
- Person 1 likes candies 0 and 1.
- Person 2 likes candies 1, 2, and 3.
- Person 3 likes candies 3 and 4.
Possible Distribution with ( T = 2 ):
- Person 1: Candies 0 and 1.
- Person 2: Candies 2 and 3.
- Person 3: Candy 4.
Output (CHIAKEO.OUT):
2
This indicates that the minimum ( T ) required to distribute all candies is 2.
Conclusion:
This solution efficiently determines the minimal ( T ) using a combination of binary search and the max-flow approach. Dinic's algorithm ensures that the flow computations are done swiftly, making it suitable for the given constraints.