void topological_sort(int num_nodes)
{
  for (int i = 0; i < num_nodes; ++i) {
    if (incoming_degree.find(i) == incoming_degree.end()) pq.push(i);
  }

  while (!pq.empty()) {
    int u = pq.top(); pq.pop();
    printf("%d ", u);

    for (int i = 0; i < adj_list[u].size(); ++i) {
      if (--incoming_degree[adj_list[u][i]] == 0)
        pq.push(adj_list[u][i]);
    }

    adj_list[u].clear();
  }
}
} inline void addPoint(mii &a, const point &p) { // `no collinear points`
	int x = p.x, y = p.y; mit pnt = a.insert(make_pair(x, y)).first, p1, p2;
	for (pnt->y = y; ; a.erase(p2)) {
		p1 = pnt; if (++p1 == a.end()) break;
		p2 = p1;  if (++p1 == a.end()) break;
		if (det(point(p2) - p, point(p1) - p) < 0) break;
	} for ( ; ; a.erase(p2)) {
		if ((p1 = pnt) == a.begin()) break;
		if (--p1 == a.begin()) break; p2 = p1--;
		if (det(point(p2) - p, point(p1) - p) > 0) break;
	}
}
int main()
{
    int cases;
    cin >> cases;
    
    int tn;
    for(int tcase = 1; tcase <= cases; ++tcase)
    {
        s1.clear(); 
        s2.clear();
        adjList.clear();
        adjMat.clear();
        parent.clear();
        s1Size = 0;
        s2Size = 0;
        start = 0;
        sink = 0;
        
        
        // Get input, Build adjacency list + matrix
        cin >> s1Size;
        for(int n = 0; n < s1Size; ++n)
        {
            scanf("%i", &tn);
            s1.insert(tn);
            adjList[tn] = si();
            adjMat[tn] = mii();
        }
        
        cin >> s2Size;
        for(int n = 0; n < s2Size; ++n)
        {
            scanf("%i", &tn);
            s2.insert(tn);
            adjList[tn] = si();
            adjMat[tn] = mii();
            
            for(si_it a = s1.begin(); a != s1.end(); ++a)
                if((tn == 0 && *a == 0) || tn == 0 || 
                   (*a != 0 && tn % (*a) == 0))
                {
                    adjList[*a].insert(tn);
                    adjMat[*a][tn] = 1;
                    adjMat[tn][*a] = 1;
                }
                else
                {
                    adjMat[*a][tn] = 0;
                    adjMat[tn][*a] = 0;
                }
        }
        
        for(start = 0; s1.count(start) || s2.count(start); ++start) {}
        for(sink = 1; s1.count(sink) || s2.count(sink) || 
                      sink == start; ++sink) {}
        
        adjList[start] = si();
        adjList[sink] = si();
        adjMat[start] = mii();
        adjMat[sink] = mii();
        
        for(si_it a = s1.begin(); a != s1.end(); ++a)
        {
            adjList[start].insert(*a);
            adjMat[start][*a] = 1;
            adjMat[*a][start] = 1;
        }
        for(si_it a = s2.begin(); a != s2.end(); ++a)
        {
            adjList[*a].insert(sink);
            adjMat[*a][sink] = 1;
            adjMat[sink][*a] = 1;
        }
        
        
        // Edmonds Karp's
        int maxFlow = 0;
        
        while(true)
        {
            queue<int> bfsQ;
            set<int> visited;
            tFlow = 0;
            
            bfsQ.push(start);
            visited.insert(start);
            
            while(!bfsQ.empty())
            {
                int tn = bfsQ.front();
                bfsQ.pop();
                
                if(tn == sink)
                    break;
                    
                for(si_it a = adjList[tn].begin();
                          a != adjList[tn].end(); ++a)
                {
                    if(adjMat[tn][*a] > 0 && !visited.count(*a))
                    {
                        visited.insert(*a);
                        parent[*a] = tn;
                        bfsQ.push(*a);
                    }
                }
            }
            
            augmentPath(sink, INF);
            if(tFlow == 0)
                break;
            maxFlow += tFlow;
        }
        
        printf("Case %i: %i\n", tcase, maxFlow);
    }
    
    return 0;
}
Exemplo n.º 4
0
int main()
{
	#ifdef LOCAL
		freopen(fn".in", "r", stdin);
		freopen(fn".out", "w", stdout);
	#endif
	scanf("%I64d%I64d%I64d%I64d", &a1, &b1, &a2, &b2);
	calc(a1, d1);
	calc(b1, d1);
	calc(a2, d2);
	calc(b2, d2);

	for (auto it = begin(d1); it != end(d1); it++)
	{
		if (it -> f != 2 && it -> f != 3)
		{
			int k = it -> f;
			if (d2.find(k) == end(d2) || d2[k] != it -> s)
			{
				puts("-1");
				return 0;
			}
		}
	}
	for (auto it = begin(d2); it != end(d2); it++)
	{
		if (it -> f != 2 && it -> f != 3)
		{
			int k = it -> f;
			if (d1.find(k) == end(d1) || d1[k] != it -> s)
			{
				puts("-1");
				return 0;
			}
		}
	}

	ll delta1 = 0, delta2 = 0;
	if (d2[3] > d1[3])
	{
		delta1 = d2[3] - d1[3];
		d2[3] -= delta1;
		d2[2] += delta1;
		ch(a2, b2, delta1);
	}
	else
	{
		delta1 = d1[3] - d2[3];
		d1[3] -= delta1;
		d1[2] += delta1;
		ch(a1, b1, delta1);
	}
	if (d2[2] > d1[2])
	{
		delta2 = d2[2] - d1[2];
		d2[2] -= delta2;
		change(a2, b2, pow(2, delta2));
	}
	else
	{
		delta2 = d1[2] - d2[2];
		d1[2] -= delta2;
		change(a1, b1, pow(2, delta2));
	}
	printf("%I64d\n", delta1 + delta2);
	printf("%I64d %I64d\n%I64d %I64d", a1, b1, a2, b2);
}
Exemplo n.º 5
0
int main()
{
	fibPos[1] = 1;
	for(int i = 2; i < 46; ++i)
	{
		
		fib[i] = fib[i-2] + fib[i-1];
		fibPos[ fib[i] ] = i;
		//printf("%d: %u %u %lld \n", i, fib[i], 1U << 31, (ll) (1U << 31) - (ll) fib[i] );
	}
	
	int T;
	scanf("%d", &T);
	
	while(T--)
	{
		int nPos;
		scanf("%d", &nPos);

		int fibNum;
		int maxPos = 0;
		for(int i = 0; i < nPos; ++i)
		{
			scanf("%d", &fibNum);
			mii::iterator mit = fibPos.find(fibNum);
			pos[i] = mit->second;
			maxPos = max(maxPos, pos[i]);
			//printf("fibNum %d  pos=%d\n", fibNum, pos[i]);
		}
		
		gets(buf);
		int len = strlen(buf);
		if (len == 0)
		{
			gets(buf);
			len = strlen(buf);
		}
		//printf("%s\n", buf);
		
		for(int i = 0; i < maxPos; ++i)
		{
			ans[i] = ' ';
		}
		ans[maxPos] = '\0';
		
		int ansPos = 0;
		for(int i = 0; i < len; ++i)
		{			
			if (buf[i] < 'A' || buf[i] > 'Z')
				continue;
			
			//printf("Ans pos %d pos[] = %d  i=%d %c\n", ansPos, pos[ansPos], i, buf[i]);
			ans[ pos[ansPos] - 1 ] = buf[i];
			++ansPos;
			if (ansPos >= nPos)
				break;
		}
		
		printf("%s\n", ans);
		//printf("[%s]  maxPos %d\n", ans, maxPos);
	}

	return 0;
}
inline bool checkInside(mii &a, const point &p) { // `border inclusive`
	int x = p.x, y = p.y; mit p1 = a.lower_bound(x);
	if (p1 == a.end()) return false; if (p1->x == x) return y <= p1->y;
	if (p1 == a.begin()) return false; mit p2(p1--);
	return sign(det(p - point(p1), point(p2) - p)) >= 0;
} inline void addPoint(mii &a, const point &p) { // `no collinear points`