void stableMatching (int n, VVI& maleRank, VVI& femaleRank, VI& wife) {
    // a male m prefers w to w' if maleRank[m][w] < maleRank[m][w']
    // returns male-optimal matching

    VI freeMen;
    VVPI fq(n);
    VI husband(n, -1);
    for (int m = 0; m < n; ++m) {
        for (int w = 0; w < n; ++w) {
            fq[m].push_back(make_pair(maleRank[m][w], w));
        }
        sort(all(fq[m]), greater<PI>());
        freeMen.push_back(m);
    }
    while (!freeMen.empty()) {
        int m = freeMen.back(), w = fq[m].back().y;
        fq[m].pop_back();
        if (husband[w] == -1) {
            husband[w] = m;
            freeMen.pop_back();
        } else if (femaleRank[w][m] < femaleRank[w][husband[w]]) {
            freeMen.pop_back();
            freeMen.push_back(husband[w]);
            husband[w] = m;
        }
    }
    wife = VI(n);
    for (int w = 0; w < n; ++w) {
        wife[husband[w]] = w;
    }
}
    void DFS (int v) {
        idx[v] = index;
        low[v] = index;
        index += 1;
        st.push_back(v);
        inStack[v] = true;

        for (auto w : adj[v]) {
            if (idx[w] == -1) {
                DFS(w);
                low[v] = min(low[v], low[w]);
            } else if (inStack[w]) {
                low[v] = min(low[v], low[w]);
            }
        }

        if (low[v] == idx[v]) {
            int w;
            components.push_back(VI());
            do {
                w = st.back();
                st.pop_back();
                inStack[w] = false;
                componentOf[w] = totalComponents;
                components[totalComponents].push_back(w);
            } while (w != v);
            totalComponents++;
        }
    }
bool solve(int n, int k) {
   if (k == 1)
      return true;
   if (k > MAXK)
      return false;
   if (fact[k] > n)
      return false;

   VI v;
   int N = n;
   for (VI::iterator it = primes.begin(); it != primes.end() && (*it) * (*it) <= N; ++it)
      if (N % *it == 0) {
         v.push_back(0);
         while (N % *it == 0) {
            ++v.back();
            N /= *it;
         }
      }
   if (N > 1)
      v.push_back(1);
   sort(v.begin(), v.end());
   
   reverse(v.begin(), v.end());
   k -= (int) v.size() + 1;
   for (VI::iterator it = v.begin(); it != v.end(); ++it)
      *it -= 1;
   while (!v.empty() && v.back() == 0) {
      v.pop_back();
   }

   if (k <= 0)
      return true;

   if (DBG + 0) {
      cerr << "BEGIN!!!!!\n";
      for (VI::iterator it = v.begin(); it != v.end(); ++it)
         cerr << *it << " -- ";
      cerr << endl;
      cerr << k << endl;
   }
   VI w(v.size(), 1);
   bool found = walk1(v, w, 0, k/*,set<VI>()*/);

   if (!found) {
      if (v.size() >= 3 && v[2] >= 3) {
         v[0] -= 1;
         v[1] -= 1;
         v[2] -= 1;
         found = walk1(v, w, 0,  k - 1/*,set<VI>()*/);
      }
   }

   return found;
}
示例#4
0
文件: d.cpp 项目: Yan-Song/burdakovd
pii minimum_odd()
{
	pii ret(-1,-1);
	if(!ones.empty()){
		ret = pii(1,ones.back());
		ones.pop_back();
	}
	else
		if(!odd.empty())
		{
			ret = odd.top();
			odd.pop();
		}
	return ret;
}
示例#5
0
	void dfs(int depth, VI& nums, VI& cur, USET_VI& uset, VVI& result) {
		if (depth == nums.size()) {
			if (check(cur)) {
				string s = toString(cur);
				if (uset.find(s) == uset.end()) {
					uset.insert(s);
					result.push_back(cur);
				}
			}
		}
		else {
			dfs(depth + 1, nums, cur, uset, result);
			cur.push_back(nums[depth]);
			dfs(depth + 1, nums, cur, uset, result);
			cur.pop_back();
		}
	}
void DFS(){
    visited.resize(k,0);
    REP(v,k) if (!visited[v]){
        visited[v] = 1;
        printf("%d 0\n",v+1);
        stack.PB(v);
        while (!(stack.empty())){
            int u = stack.back();
            stack.pop_back();
            for(auto ngb : sol[u])
                if (!visited[ngb]){
                    visited[ngb] = 1;
                    printf("%d %d\n",ngb+1,u+1);
                    stack.PB(ngb);
                }
        }    
    }
}
示例#7
0
void dfs(int u,int l){
  vs[u] = 1;
  d[u] = l;
  v.PB(u);
  for(int i=0;i<SZ(g[u]);i++){
    int w = g[u][i];
    if(!vs[w]){
      p[w] = u;
      dfs(w,l+1);
    }
    else{
      if(w!=p[u]){
       for(int j=d[w];j<SZ(v);j++){
         m[v[j]] = 1;
       }
      }
    }
  }
  v.pop_back();
}
示例#8
0
  int f(VI Q, VI R) {
    int rem=0;
    
    while(SZ(Q)<SZ(R)) Q.push_back(0);
    while(SZ(Q)>SZ(R)) rem+=Q.back(), Q.pop_back();

    assert(SZ(Q)==SZ(R));
    
    int debt=0;
    REP(i,SZ(R)) {
      if(R[i]>Q[i]) {
        debt+=R[i]-Q[i];
        continue;
      }
      if(R[i]<Q[i]) {
        rem+=Q[i]-R[i];
      }
    }

    return debt>rem?Inf:debt;
  }
    int DFS (PI v, int index) {
        idx[v.x] = index;
        low[v.x] = index;
        index += 1;

        int children = 0;
        bool ap = false;
        for (auto w : adj[v.x]) if (w.y != v.y) {
                if (idx[w.x] == -1) {
                    st.push_back(w.y);
                    index = DFS(w, index);
                    low[v.x] = min(low[v.x], low[w.x]);
                    if (low[w.x] > idx[v.x]) {
                        bridges.push_back(w.y);
                    }
                    children++;
                    if (low[w.x] >= idx[v.x]) {
                        if (v.y != -1 || children >= 2) {
                            ap = true;
                        }
                        components.push_back(VI());
                        totalComponents++;
                        int u;
                        do {
                            u = st.back();
                            st.pop_back();
                            components.back().push_back(u);
                        } while (u != w.y);
                    }
                } else if (idx[w.x] < idx[v.x]) {
                    st.push_back(w.y);
                    low[v.x] = min(low[v.x], idx[w.x]);
                }
            }
        if (ap) {
            cutVertices.push_back(v.x);
        }
        return index;
    }
示例#10
0
	int minValue(int N, int K, vector <int> s) {
		int m = SZ(s);
		int sum = 0;
		REP(i, m) sum += s[i];
		if (sum <= (K - 1) * N) return 0;
		else {
			int tot = N;
			int ans = 0;
			VI q;
			REP(i, m) if (s[i]) q.push_back(s[i]);
			sort(ALL(q));
			reverse(ALL(q));
			while (sum > (K - 1) * N && N > 0) {
				sum -= SZ(q);
				REP(i, SZ(q)) q[i]--;
				while (SZ(q) && q.back() == 0) q.pop_back();
				N--;
				ans++;
			}
			return ans;
		}
	}